簡體   English   中英

如何遍歷 dataframe 以格式化值?

[英]How to iterate through a dataframe to format values?

我有一個 dataframe ,其中每一列都有像5,12; 3,14; 12,01... 5,12; 3,14; 12,01... 12,01 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。 我希望有人能給我一個關於如何修復它的提示。

您需要遍歷每一列,將每一列轉換為字符串(使用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

也許你可以嘗試這樣的事情:

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 function 應用於您需要的每一列,然后使用 pd.to_numeric 將這些列中的每一列轉換為浮點數。 output 看起來像這樣:

     INS  GEN_POWER1  GEN_POWER2
0   1.12        3.14         NaN
1  12.10      234.10        21.1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM