简体   繁体   English

以两对的形式转置多列-pandas python

[英]Transpose multiple columns in pairs of two - pandas python

I would like to transpose multiple columns in pairs of two我想转置多列成对两列

I have the following columns:我有以下几列:

user_id', 'fullname', 'email', 'handle', 'audience_ethnicities_code0', 'audience_ethnicities_weight0', 'audience_ethnicities_code1', 'audience_ethnicities_weight1', 'audience_ethnicities_code2', 'audience_ethnicities_weight2', 'audience_ethnicities_code3', 'audience_ethnicities_weight3'

where code and weigh are related, for example:其中代码和权重相关,例如:

user_id = ABCD用户 ID = ABCD

'audience_ethnicities_code0' = asian;
'audience_ethnicities_weight0' = 0.4 

'audience_ethnicities_code1' = african; 
'audience_ethnicities_weight1' = 0.2

'audience_ethnicities_code2' = white;
'audience_ethnicities_weight2' = 0.2 

'audience_ethnicities_code3' = hispanic; 
'audience_ethnicities_weight3' = 0.2

tot weight = 1, and the audience of user ABCD is 40% Asian, 20% African etc. What I want is to have ethnicity ( audience_ethnicities_code_n ) in the column and in the row their weight ( audience_ethnicities_weight_n ) for each user总权重 = 1,用户 ABCD 的受众是 40% 亚洲人,20% 非洲人等。我想要的是在列和行中为每个用户设置种族( audience_ethnicities_code_n _种族_代码_n)他们的权重( audience_ethnicities_weight_n _种族_权重audience_ethnicities_weight_n

I tried this query but it gave me a messy result:我试过这个查询,但它给了我一个混乱的结果:

df1 = df.pivot_table(index=['user_id', 'fullname', 'email', 'handle'], 
                    columns=['audience_ethnicities_code0', 'audience_ethnicities_code1', 'audience_ethnicities_code2', 'audience_ethnicities_code3'], 
                    values=['audience_ethnicities_weight0', 'audience_ethnicities_weight1', 'audience_ethnicities_weight2', 'audience_ethnicities_weigh3'], aggfunc=lambda x: ' '.join(str(v) for v in x))

df1

any ideas?有任何想法吗?

I would iteratively do the pivot for each column and then merge the dataframes by their index.我会迭代地为每一列做数据透视,然后通过它们的索引合并数据帧。

Here an example:这里有一个例子:

from functools import reduce

index = ['user_id', 'fullname', 'email', 'handle']

dfList = []
for i in range(3):
  dfList.append(df.pivot_table(index=index, 
                               columns='audience_ethnicities_code{}'.format(i), 
                               values='audience_ethnicities_weight{}'.format(i))
                  .rename_axis(None, axis=1)
                  .reset_index())

reduce(lambda x, y: pd.merge(x, y, on=index), dfList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM