![](/img/trans.png)
[英]Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas
[英]Merge row with a same column value based on other column values in pandas
我有一個 dataframe 看起來像這樣:
data = {'Id':["A", "A", "A", "B", "B","B"],
'extraction': ["apple", "cherry", "alfatoxin", "ethyl", "glyphosate", "pasta"],
"entities": ["food", "food", "hazard", "hazard", "hazard", "food"]}
df = pd.DataFrame(data)
Id extraction entities
0 A apple food
1 A cherry food
2 A alfatoxin hazard
3 B ethyl hazard
4 B glyphosate hazard
5 B pasta food
我想要的 output 是:
Id food hazards
0 A [apple, cherry] alfatoxin
1 B pasta [ethyl, glyphosate]
我如何在 pandas 中執行此操作? groupby 是一個不錯的選擇嗎?
使用帶有聚合的pivot_table
表作為list
:
df.pivot_table(index='Id', columns='entities', values='extraction', aggfunc=list)
Output:
entities food hazard
Id
A [apple, cherry] [alfatoxin]
B [pasta] [ethyl, glyphosate]
或者,讓單個項目不作為列表:
df.pivot_table(index='Id', columns='entities', values='extraction',
aggfunc=lambda x: list(x) if len(x)>1 else x.squeeze())
Output:
entities food hazard
Id
A [apple, cherry] alfatoxin
B pasta [ethyl, glyphosate]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.