問題描述
我從列表列表中創建了一個 pandas 數據框
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
有沒有辦法將數據框的所有列轉換為可以轉換的浮點數,即 B 和 C?如果您知道要轉換哪些列,則可以使用以下方法:
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)
轉換兩列(雖然B"是 int
而C"是float",因為存在 NaN
值),但是
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)
顯然會引發錯誤消息,我看不出有什么方法可以抑制它.
是否有可能在不遍歷每一列的情況下執行此字符串-浮點轉換,以嘗試 .astype("float", errors = "ignore")
?
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")
?
推薦答案
我覺得你需要errors='ignore'pandas-docs/stable/generated/pandas.to_numeric.html" rel="noreferrer">to_numeric
:
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
您也可以將 int
向下轉換為 float
s:
You can downcast also int
to float
s:
df = df.apply(pd.to_numeric, errors='ignore', downcast='float')
print (df.dtypes)
A object
B float32
C float32
dtype: object
同理:
df = df.apply(lambda x: pd.to_numeric(x, errors='ignore', downcast='float'))
print (df.dtypes)
A object
B float32
C float32
dtype: object
這篇關于在所有 pandas 列中將字符串轉換為浮點數,這是可能的的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!