简体   繁体   English

如何将带有浮点数、整数和字符串的数据转换为字符串和浮点数

[英]how to convert data with float, int and strings to just strings and float

I have a csv with data in different formats.我有一个包含不同格式数据的 csv。 there are float, int and strings.有浮点数、整数和字符串。

How can I modify just the int into float?如何仅将 int 修改为 float?

I have not tried much, just with this:我没有尝试太多,只是这样:

df=df.astype(dtype=np.float64, copy=True, errors='raise')

but cant manage to make it work, because there are also strings但无法使其工作,因为还有字符串

How can I solve this?我该如何解决这个问题?

The columns of a pandas DataFrame (or a Series) are homogeneously of type. pandas DataFrame(或系列)的列是同质类型的。 You can inspect this with dtype (or DataFrame.dtypes)您可以使用 dtype(或 DataFrame.dtypes)检查它

You can select_dtypes first and and finally convert to float using df.astype您可以先 select_dtypes,最后使用 df.astype 转换为浮点数

tmp = df.select_dtypes(include=['Int64'])
df[tmp.columns]= tmp.astype('float64')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM