3 回答

TA貢獻1817條經驗 獲得超14個贊
您可以使用get_dummies此處有效地執行此操作:
dummies = (df['allies'].str.get_dummies(sep=', ')
.reindex(df['country'].unique(), axis=1)
.add_suffix('_ally'))
df.join(dummies)
country allies USA_ally China_ally Singapore_ally
0 USA Turkey, UK, France, India 0 0 0
1 China DPRK, Singapore 0 0 1
2 Singapore USA, China 1 1 0
在哪里,
dummies
USA_ally China_ally Singapore_ally
0 0 0 0
1 0 0 1
2 1 1 0

TA貢獻1813條經驗 獲得超2個贊
讓我們試試這個,用它series.unique來識別獨特的國家,然后str.contains檢查它是否存在。
for c in df.country.unique():
df[f'{c}_Aally'] = df.allies.str.contains(c).astype(int)
df
Out[20]:
country allies USA_Aally China_Aally Singapore_Aally
0 USA Turkey, UK, France, India 0 0 0
1 China DPRK, Singapore 0 0 1
2 Singapore USA, China 1 1 0

TA貢獻2016條經驗 獲得超9個贊
這是您的代碼的概括,首先獲取列中出現的所有唯一字母letter,然后分別循環遍歷它們并基本上對每個字母執行您在上面所做的事情。
complete_letter_set = set(''.join(df['letter'])
for l in complete_letter_set:
df[f"letter{l}exists"] = df['letter'].map(lambda x: int(l in x))
請注意,我已將條件簡化1 if A in x else 0為 just int(l in x),因為int(True) == 1無論如何int(False) == 0。
添加回答
舉報