import pandas as pd d2 = {"col1": ['1000', '$20', np.nan, 40, '$50', '67'], "col2": ['1,000', '$20', np.nan, 40, '$50', '67']} df = pd.DataFrame(d2) print(df.dtypes) def clean_price_column(df, column): df2 = df.copy() df2['col1'] = df2['col1'].replace({'\$': '',',': ''}, regex=True).astype(float) print(df2.dtypes) return df2 test_case = clean_price_column(df, 'col1') print(df.dtypes) in the function clean_price_column(df, 'col1') df2 col1 type is float, and after the function is called using df, the df col1 type is still object. what do I need to do to make df col1 data type is float after clean_price_column(df, 'col1') is called? thanks col1 object col2 object dtype: object col1 float64 col2 object dtype: object col1 object col2 object dtype: object
in the function clean_price_column(df, 'col1') df2 col1 type is float, and after the function is called using df, the df col1 type is still object. what do I need to do to make df col1 data type is float after clean_price_column(df, 'col1') is called? thanks col1 object col2 object dtype: object col1 float64 col2 object dtype: object col1 object col2 object dtype: object
你最后应该print(test_case.dtypes)
thanks, got it.