[英]Merge cells in pandas if values in two column is same
I have the following data in terms of dataframe我在数据框方面有以下数据
data = pd.DataFrame({'colA': ['a', 'c', 'a', 'e', 'c', 'c'], 'colB': ['b', 'd', 'b', 'f', 'd', 'd'], 'colC':['SD100', 'SD200', 'SD300', 'SD400', 'SD500', 'SD600']})
I want the output as attached [enter image description here][2]我想要附加的输出[在此处输入图像描述][2]
I want to achieve this using pandas dataframe in python Can somebody help me?我想在 python 中使用 Pandas 数据框来实现这一点 有人可以帮助我吗?
You can try:你可以试试:
Column A Column B Column C
0 a b SD100
1 c d SD200
2 a b SD300
3 e f SD400
4 c d SD500
5 c d SD600
>>> df.groupby(['Column A', 'Column B']).agg(list)
Column C
Column A Column B
a b [SD100, SD300]
c d [SD200, SD500, SD600]
e f [SD400]
I don't know why you want to make multindex, but you can simply sort_values
or use groupby
.我不知道你为什么要制作 multindex,但你可以简单地sort_values
或使用groupby
。
import pandas as pd
df = pd.DataFrame({"ColumnA":['a','c','a','e','c','c'],
"ColumnB":['b','d','b','f','d','d'],
"ColumnC":['SD100','SD200','SD300','SD400','SD500','SD600']})
print(df)
ColumnA ColumnB ColumnC
0 a b SD100
1 c d SD200
2 a b SD300
3 e f SD400
4 c d SD500
5 c d SD600
df = df.sort_values(by=['ColumnA','ColumnB'])
df.set_index(['ColumnA', 'ColumnB','ColumnC'], inplace=True)
df
这会将您的数据更新为您想要的
data=data.groupby(['colA','colB']).agg(list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.