問(wèn)題描述
我從列表列表中創(chuàng)建了一個(gè) pandas 數(shù)據(jù)框
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
有沒(méi)有辦法將數(shù)據(jù)框的所有列轉(zhuǎn)換為可以轉(zhuǎn)換的浮點(diǎn)數(shù),即 B 和 C?如果您知道要轉(zhuǎn)換哪些列,則可以使用以下方法:
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")
但是,如果您事先不知道哪些列包含數(shù)字,您會(huì)怎么做?當(dāng)我嘗試時(shí)
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")
所有列仍然是字符串/對(duì)象.同樣,
all columns are still strings/objects. Similarly,
df[["B", "C"]] = df[["B", "C"]].apply(pd.to_numeric)
轉(zhuǎn)換兩列(雖然B"是 int
而C"是float",因?yàn)榇嬖?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)
顯然會(huì)引發(fā)錯(cuò)誤消息,我看不出有什么方法可以抑制它.
是否有可能在不遍歷每一列的情況下執(zhí)行此字符串-浮點(diǎn)轉(zhuǎn)換,以嘗試 .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")
?
推薦答案
我覺(jué)得你需要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
如果不是混合值,它工作得很好 - 帶有字符串的數(shù)字:
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
向下轉(zhuǎn)換為 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
這篇關(guān)于在所有 pandas 列中將字符串轉(zhuǎn)換為浮點(diǎn)數(shù),這是可能的的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!