Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

python - Convert strings to float in all pandas columns, where this is possible

I created a pandas dataframe from a list of lists

import pandas as pd

df_list = [["a", "1", "2"], ["b", "3", np.nan]]
df = pd.DataFrame(df_list, columns = list("ABC"))
>>>   A  B    C
   0  a  1    2
   1  b  3  NaN

Is there a way to convert all columns of the dataframe to float, that can be converted, i.e. B and C? The following works, if you know, which columns to convert:

  df[["B", "C"]] = df[["B", "C"]].astype("float")

But what do you do, if you don't know in advance, which columns contain the numbers? When I tried

  df = df.astype("float", errors = "ignore")

all columns are still strings/objects. Similarly,

df[["B", "C"]] = df[["B", "C"]].apply(pd.to_numeric)

converts both columns (though "B" is int and "C" is "float", because of the NaN value being present), but

df = df.apply(pd.to_numeric)

obviously throws an error message and I don't see a way to suppress this.
Is there a possibility to perform this string-float conversion without looping through each column, to try .astype("float", errors = "ignore")?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you need parameter errors='ignore' in to_numeric:

df = df.apply(pd.to_numeric, errors='ignore')
print (df.dtypes)
A     object
B      int64
C    float64
dtype: object

It working nice if not mixed values - numeric with strings:

df_list = [["a", "t", "2"], ["b", "3", np.nan]]
df = pd.DataFrame(df_list, columns = list("ABC"))

df = df.apply(pd.to_numeric, errors='ignore')
print (df)
   A  B    C
0  a  t  2.0 <=added t to column B for mixed values
1  b  3  NaN

print (df.dtypes)
A     object
B     object
C    float64
dtype: object

EDIT:

You can downcast also int to floats:

df = df.apply(pd.to_numeric, errors='ignore', downcast='float')
print (df.dtypes)
A     object
B    float32
C    float32
dtype: object

It is same as:

df = df.apply(lambda x: pd.to_numeric(x, errors='ignore', downcast='float'))
print (df.dtypes)
A     object
B    float32
C    float32
dtype: object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...