转置数据框中的两列或多列

我有一个看起来像的数据框:


PRIO   Art  Name      Value

1      A     Alpha     0

1      A     Alpha     0

1      A     Beta      1

2      A     Alpha     3

2      B     Theta     2 

我如何转置数据框,我将所有唯一名称作为一列,并具有相应的值(请注意,我想忽略重复的行)?所以在这种情况下:


PRIO   Art  Alpha      Alpha_value  Beta   Beta_value  Theta Theta_value

1      A    1             0         1       1           NaN    NaN

2      A    1             3         NaN     NaN         NaN    NaN

2      B    NaN           NaN       NaN     NaN          1     2


白猪掌柜的
浏览 129回答 3
3回答

开心每一天1111

这是使用pivot_table.&nbsp;要记住一些棘手的事情:您需要将两者都指定'PRIO', 'Art'为数据透视索引我们还可以使用两个聚合函数在一次调用中完成我们必须重命名 0 级列以区分它们。所以你需要交换级别并重命名out = df.pivot_table(index=['PRIO', 'Art'], columns='Name', values='Value',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aggfunc=[lambda x: 1, 'first'])# get the column names rightd = {'<lambda>':'is_present', 'first':'value'}out = out.rename(columns=d, level=0)out.columns = out.swaplevel(1,0, axis=1).columns.map('_'.join)print(out.reset_index())&nbsp; &nbsp;PRIO Art&nbsp; Alpha_is_present&nbsp; Beta_is_present&nbsp; Theta_is_present&nbsp; Alpha_value&nbsp; \0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp;A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp;A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.0&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp;B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp;&nbsp; &nbsp;Beta_value&nbsp; Theta_value&nbsp;&nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp;&nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp;&nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.0

千万里不及你

pd.crosstab()这是和的示例groupby()。df = pd.concat([pd.crosstab([df['PRIO'],df['Art']], df['Name']),df.groupby(['PRIO','Art','Name'])['Value'].sum().unstack().add_suffix('_value')],axis=1).reset_index()df|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp; &nbsp;Alpha |&nbsp; &nbsp;Beta |&nbsp; &nbsp;Theta |&nbsp; &nbsp;Alpha_value |&nbsp; &nbsp;Beta_value |&nbsp; &nbsp;Theta_value ||:---------|--------:|-------:|--------:|--------------:|-------------:|--------------:|| (1, 'A') |&nbsp; &nbsp; &nbsp; &nbsp;1 |&nbsp; &nbsp; &nbsp; 1 |&nbsp; &nbsp; &nbsp; &nbsp;0 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nan || (2, 'A') |&nbsp; &nbsp; &nbsp; &nbsp;1 |&nbsp; &nbsp; &nbsp; 0 |&nbsp; &nbsp; &nbsp; &nbsp;0 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nan |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nan || (2, 'B') |&nbsp; &nbsp; &nbsp; &nbsp;0 |&nbsp; &nbsp; &nbsp; 0 |&nbsp; &nbsp; &nbsp; &nbsp;1 |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nan |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nan |&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 |

元芳怎么了

Groupby 两次,首先以 Name 和 suffix 为中心值。Next groupby 相同的命令并找到唯一的值。加入两者。在加入中,删除重复的列并根据需要重命名其他列g=df.groupby([ 'Art','PRIO', 'Name'])['Value'].\first().unstack().reset_index().add_suffix('_value')print(g.join(df.groupby(['PRIO', 'Art','Name'])['Value'].\&nbsp; &nbsp; &nbsp; &nbsp;nunique().unstack('Name').reset_index()).drop(columns=['PRIO_value','Art'])\&nbsp; &nbsp; &nbsp; .rename(columns={'Art_value':'Art'}))&nbsp;Name Art&nbsp; Alpha_value&nbsp; Beta_value&nbsp; Theta_value&nbsp; PRIO&nbsp; Alpha&nbsp; Beta&nbsp; Theta0&nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; 1.0&nbsp; &nbsp;1.0&nbsp; &nbsp; NaN1&nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; 1.0&nbsp; &nbsp;NaN&nbsp; &nbsp; NaN2&nbsp; &nbsp; &nbsp; B&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.0&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; NaN&nbsp; &nbsp;NaN&nbsp; &nbsp; 1.0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python