將包含“10%”和“0.10”型別字串的 Pandas 系列轉換為數值的最佳方法是什么?
我知道,如果我有一個只有“0.10”型別字串的系列,我就可以做到pd.to_numeric
。
我也知道,如果我有一系列“10%”型別的字串,我可以做str.replace("%","")
然后pd.to_numeric
除以 100。
我遇到的問題是一個混合了“0.10”和“10%”型別字串的系列。如何最好地將其轉換為具有正確數字型別的系列。
我想我可以通過首先制作一個帶有 True / False 的臨時系列來做到這一點,具體取決于字串中是否包含“%”,然后基于應用函式。但這似乎效率低下。
有沒有更好的辦法?
我嘗試過的參考:
mixed = pd.Series(["10%","0.10","5.5%","0.02563"])
mixed.str.replace("%","").astype("float")/100
0 0.100000
1 0.001000
2 0.055000
3 0.000256
dtype: float64
# This doesn't work, because even the 0.10 and 0.02563 are divided by 100.
uj5u.com熱心網友回復:
基于此答案的一個非常簡潔的解決方案是:
from pandas import Series, to_numeric
mixed = Series(["10%", "0.10", "5.5%", "0.02563"])
print(to_numeric(mixed.str.replace("%", "e-2")))
# 0 0.10000
# 1 0.10000
# 2 0.05500
# 3 0.02563
# dtype: float64
uj5u.com熱心網友回復:
不知何故,你需要一個條件。這是一種可能的方式:
l = pd.Series((float(x.strip('%'))/100 if '%' in x else float(x) for x in mixed))
print(l)
0 0.10000
1 0.10000
2 0.05500
3 0.02563
dtype: float64
uj5u.com熱心網友回復:
最簡單的解決方案是使用掩碼選擇條目并批量處理它們:
from pandas import Series, to_numeric
mixed = Series(["10%", "0.10", "5.5%", "0.02563"])
# make an empty series with similar shape and dtype float
converted = Series(index=mixed.index, dtype='float')
# use a mask to select specific entries
mask = mixed.str.contains("%")
converted.loc[mask] = to_numeric(mixed.loc[mask].str.replace("%", "")) / 100
converted.loc[~mask] = to_numeric(mixed.loc[~mask])
print(converted)
# 0 0.10000
# 1 0.10000
# 2 0.05500
# 3 0.02563
# dtype: float64
uj5u.com熱心網友回復:
mixed = mixed.apply(lambda x: float(x[:-1])/100 if '%' in x else float(x))
輸出:
0 0.10000
1 0.10000
2 0.05500
3 0.02563
dtype: float64
uj5u.com熱心網友回復:
嘗試:
mixed = pd.Series(["10%", "0.10", "5.5%", "0.02563"])
mixed = mixed.str.replace("%", "e-02")
print(pd.to_numeric(mixed))
印刷:
0 0.10000
1 0.10000
2 0.05500
3 0.02563
dtype: float64
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488832.html
上一篇:如何使用ncurses接收字串?