我有这样一个需求,按照'Gender'列的非重复值进行合并单元格,其他列都不变,网上都是教你合并,要把其他列进行操作。
求大佬支支招~
代码语言:txt复制#定义dataframe
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily', 'Frank', 'Grace', 'Henry', 'Ivan', 'Jack'],
'Gender': ['F', 'M', 'M', 'M', 'F', 'M', 'F', 'M', 'M', 'M'],
'Age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70],
'City': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Beijing', 'Shanghai', 'Beijing', 'Shenzhen', 'Guangzhou', 'Shanghai'],
'Salary': [5000, 8000, 6000, 7000, 9000, 5500, 7500, 6500, 7000, 10000]}
df = pd.DataFrame(data)
代码语言:txt复制#dataframe输出:
Name Gender Age City Salary
0 Alice F 25 Beijing 5000
1 Bob M 30 Shanghai 8000
2 Charlie M 35 Guangzhou 6000
3 David M 40 Shenzhen 7000
4 Emily F 45 Beijing 9000
5 Frank M 50 Shanghai 5500
6 Grace F 55 Beijing 7500
7 Henry M 60 Shenzhen 6500
8 Ivan M 65 Guangzhou 7000
9 Jack M 70 Shanghai 10000
代码语言:txt复制#按照Gender分组
df = df.groupby('Gender').agg(lambda x:x)
print(df)
#报错
ValueError: Must produce aggregated value