![](/img/trans.png)
[英]Merge the values of two columns in a Pandas Dataframe applying a function to deduplicate and concatenate
[英]Pandas pivot dataframe where values are a function of two columns
假设我们有一个 dataframe
df = pd.DataFrame({'A': [1, 2], 'A_values': [3, 4], 'B': [5, 6], 'B_values': [7, 8]})
I want to sort of pivot this dataframe so that we have columns A
and B
suffixed with their values as index and column names in a new dataframe (ie index = ['A_1', 'A_2'], columns = ['B_5', 'B_6']
),并且这个新的 dataframe 的值将是A_values
和B_values
列上的 function 的结果。 假设 function 是一个简单的和。 对于A = 1
我们有A_values = 3
,对于B = 5
我们有B_values = 7
,因此在A_1
和 B_5 交叉点的新B_5
中我们将有3+7=10
。 在下面完成生成的 dataframe:
df_pivoted = pd.DataFrame([[3+7, 3+8], [4+7, 4+8]], index = ['A_1', 'A_2'], columns = ['B_5', 'B_6'])
在搜索了一段时间后,我没有在.pivot_table()
中找到允许将多列的 function 作为值传递的功能。 也许有更适合这种情况的方法? 任何帮助表示赞赏。
我们可以使用索引进行filter
,然后使用新键和 pivot 进行merge
(在 SQL 中也称为交叉连接)。 这里我使用groupby
和unstack
等于pivot_table
A=df.filter(like='A').assign(key=1)
B=df.filter(like='B').assign(key=1)
s=A.merge(B)
s=s.assign(value=s.A_values+s.B_values).groupby(['A','B'])['value'].sum().unstack()
s
B 5 6
A
1 10 11
2 11 12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.