簡體   English   中英

迭代和替換數據框列值

[英]Iterating and replacing dataframe column values

我有以下數據框:

df = pd.DataFrame({'category': ['High', 'Central', 'Low', 'LowCentral], 
               'outcome': ['Yes', 'No', 'Yes', 'No']})

我想要做的是按照以下方式根據類別列映射結果列:

  • 如果類別== 高,則結果= 是
  • 如果類別==中央,結果=可能
  • 如果類別== 低,則結果= 否

我試過了

for i, row in df.iterrows():
    if df.loc[i, 'category'].str.contains('High'):
       df.loc[i, 'outcome'] = 'Yes'
    elif df.loc[i, 'category'].str.contains('Central'):
       df.loc[i, 'outcome'] = 'Maybe'
    elif df.loc[i, 'category'].str.contains('Low'):
       df.loc[i, 'outcome'] = 'No'

但我收到以下錯誤:

AttributeError: 'str' object has no attribute 'str'

我還嘗試使用“地圖”功能:

df['category'] = df['outcome'].map({'High':'Yes', 'Central':'Maybe', 'Low':'No'})

但這導致第 4 行,即 LowCentral 在結果列中輸出 NaN,這是不希望的。 我想保留不會包含在映射中的結果值。

任何幫助將不勝感激!

你的術語有點混亂。 您想要的是映射category列。 你的地圖解決方案很接近

df['outcome'] = df['category'].map({'High':'Yes', 'Central':'Maybe', 'Low':'No'}).fillna(df['category'])

看看pandas.Series.replace ,考慮下面的例子

import pandas as pd
df = pd.DataFrame({'category': ['High', 'Central', 'Low', 'LowCentral'],'outcome': ['Yes', 'No', 'Yes', 'No']})
df['outcome'] = df['category'].replace({'High':'Yes','Central':'Maybe','Low':'No'})
print(df)

輸出

     category     outcome
0        High         Yes
1     Central       Maybe
2         Low          No
3  LowCentral  LowCentral

請注意,未知數保持不變

試試這個

import pandas as pd

df = pd.DataFrame({'category': ['High', 'Central', 'Low', 'LowCentral'], 
               'outcome': ['Yes', 'No', 'Yes', 'No']})
               
for i, row in df.iterrows():
    if   'High'    in df.loc[i, 'category']:
       df.loc[i, 'outcome'] = 'Yes'
    elif 'Low'     in df.loc[i, 'category']:
       df.loc[i, 'outcome'] = 'No'
    elif 'Central' in df.loc[i, 'category']:
       df.loc[i, 'outcome'] = 'Maybe'   
print(df)

[輸出]

     category outcome
0        High     Yes
1     Central   Maybe
2         Low      No
3  LowCentral      No

暫無
暫無

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

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