我有以下資料框:
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,這是不希望的。我想保留不會包含在映射中的結果值。
任何幫助將不勝感激!
uj5u.com熱心網友回復:
你的術語有點混亂。你想要什么是映射的category列。你的地圖解決方案很接近
df['outcome'] = df['category'].map({'High':'Yes', 'Central':'Maybe', 'Low':'No'}).fillna(df['category'])
uj5u.com熱心網友回復:
看看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
請注意,未知數保持不變
uj5u.com熱心網友回復:
試試這個。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/340527.html
上一篇:冒號和空格后的熊貓正則運算式大寫
