其他人已经使用.astype()回答了注释中的问题,但是这里有一些代码来解释它。
只是一个提示,请提供入门代码来创建示例数据框,以使人们更轻松地为您提供帮助。
import pandas as pd
# step 1: create sample dataframe
df = pd.DataFrame({'strain': ['10.123456789', '10.23456789', '10.3456789'],
'temp': ['1.7', '1.8', '1.9'],
'weight': ['100.4', '100.5', '100.6'],
})
# step 2: examine dataframe and dtypes
print('Dataframe: ')
print(df.head())
print()
print('Dtypes: ')
print(df.dtypes)
print()
# step 3: convert the 'strain' column to float, round to 2 decimal places
df['strain_float'] = df['strain'].astype(float).round(2)
# step 4: examine dataframe and dtypes
print('Dataframe: ')
print(df.head())
print()
print('Dtypes: ')
print(df.dtypes)
print()
df
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句