5,12; 3,14; 12,01...我有一個資料框,其中每一列都有像object dtype中的數值。我想遍歷表以將 dtype 轉換為浮點數。因此,我列出了所有列名,用“。”替換“,”。每個值,然后將其轉換為正確的型別。
我的代碼如下所示:
for x in columnList:
x.replace(',' , '.')
x.astype(float)
資料:
Timestamp Ins_W/m2 GenPowerW1 GenPowerW2 GenPowerW3
2020-01-01 5,12 3,14 12,1
2020-01-02 6,84 16,4 12,1
.
.
.
不幸的是,我總是得到一個 AttributeError。我希望有人能給我一個關于如何修復它的提示。
uj5u.com熱心網友回復:
您需要遍歷每一列,將每一列轉換為字串(使用Series.str)以允許替換,然后將這些值轉換為浮點數。要將空單元格轉換為,NaN我們首先將它們替換為字串'NaN':
df = pd.DataFrame({
'Timestamp': ['2020-01-01', '2020-01-02'],
'Ins_W/m2': ['5,12', '6,84'],
'GenPowerW1': ['3,14', ''],
'GenPowerW2': ['12,1', '16,4'],
'GenPowerW3': ['', '12,1']
})
df
# Timestamp Ins_W/m2 GenPowerW1 GenPowerW2 GenPowerW3
# 0 2020-01-01 5,12 3,14 12,1
# 1 2020-01-02 6,84 16,4 12,1
columnList = ['Ins_W/m2', 'GenPowerW1', 'GenPowerW2', 'GenPowerW3']
for col in columnList :
df[col] = df[col].str.replace(',', '.').replace('', 'NaN').astype(float)
df
# Timestamp Ins_W/m2 GenPowerW1 GenPowerW2 GenPowerW3
# 0 2020-01-01 5.12 3.14 12.1 NaN
# 1 2020-01-02 6.84 NaN 16.4 12.1
df['GenPowerW1']
# 0 3.14
# 1 NaN
# Name: GenPowerW1, dtype: float64
uj5u.com熱心網友回復:
也許你可以嘗試這樣的事情:
df = pd.DataFrame([["1,12", "3,14", ""], ["12,1", "234,1", "21,1"]], columns=["INS", "GEN_POWER1", "GEN_POWER2"])
df[["INS", "GEN_POWER1", "GEN_POWER2"]] = df[["INS", "GEN_POWER1", "GEN_POWER2"]].apply(lambda x: x.str.replace(",", "."))
df[["INS", "GEN_POWER1", "GEN_POWER2"]] = df[["INS", "GEN_POWER1", "GEN_POWER2"]].apply(pd.to_numeric, errors='coerce')
這會將 lambda 函式應用于您需要的每一列,然后使用 pd.to_numeric 將這些列中的每一列轉換為浮點數。輸出如下所示:
INS GEN_POWER1 GEN_POWER2
0 1.12 3.14 NaN
1 12.10 234.10 21.1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505269.html
上一篇:在同一行列印多個物件
下一篇:在R中命名N個子串列
