python return dataframe types

d
decadence
楼主 (北美华人网)
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
1
180maidenlane
有一个网站叫做stackoverflow…
y
yangerchenamu
把 df2 = df.copy() 改为 df2 = df
C
Cumberbitch
楼主你是搞笑吗 test_case = clean_price_column(df, 'col1') print(df.dtypes)
你最后应该print(test_case.dtypes)  
d
decadence
楼主你是搞笑吗 test_case = clean_price_column(df, 'col1') print(df.dtypes)
你最后应该print(test_case.dtypes)  
Cumberbitch 发表于 2021-07-06 14:36

thanks, got it.