简体   繁体   English

Pandas:如何将 pivot 行转换为列

[英]Pandas: How to pivot rows into columns

I am looking for a way to pivot around 600 columns into rows.我正在寻找一种将 pivot 大约 600 列成行的方法。 Here's a sample with only 4 of those columns (good, bad, ok, Horrible):这是一个只有 4 个列的示例(好、坏、好、可怕):

df:东风:

RecordID   good   bad   ok  Horrible
  A          0      0    1     0 
  B          1      0    0     1

desired output:所需的 output:

 RecordID     Column    Value
   A           Good       0
   A            Bad       0
   A            Ok        1
   A          Horrible    0
   B           Good       1
   B            Bad       0
   B            Ok        0
   B          Horrible    1

You can use .stack() as follows.您可以使用.stack()如下。 Using .stack() is preferred as it naturally resulted in rows already sorted in the order of RecordID so that you don't need to waste processing time sorting on it again, especially important when you have a large number of columns .使用.stack()是首选,因为它自然会导致行已经按RecordID的顺序排序,这样您就不需要浪费处理时间再次对其进行排序当您有大量列时尤其重要

df = df.set_index('RecordID').stack().reset_index().rename(columns={'level_1': 'Column', 0: 'Value'})

Output: Output:

  RecordID    Column  Value
0        A      good      0
1        A       bad      0
2        A        ok      1
3        A  Horrible      0
4        B      good      1
5        B       bad      0
6        B        ok      0
7        B  Horrible      1

You can use melt function:您可以使用熔体 function:

(df.melt(id_vars='RecordID', var_name='Column', value_name='Value')
   .sort_values('RecordID')
   .reset_index(drop=True)
)

Output: Output:

    RecordID      Column    Value
0          A        good        0
1          A         bad        0
2          A          ok        1
3          A    Horrible        0
4          B        good        1
5          B         bad        0
6          B          ok        0
7          B    Horrible        1

Adding dataframe:添加dataframe:

import pandas as pd
import numpy as np

data2 = {'RecordID': ['a', 'b', 'c'],
        'good': [0, 1, 1],
        'bad': [0, 0, 1],
        'horrible': [0, 1, 1],
        'ok': [1, 0, 0]}
  
# Convert the dictionary into DataFrame
df = pd.DataFrame(data2)

Melt data: https://pandas.pydata.org/docs/reference/api/pandas.melt.html熔体数据: https://pandas.pydata.org/docs/reference/api/pandas.melt.html

melted = df.melt(id_vars='RecordID', var_name='Column', value_name='Value')
melted

在此处输入图像描述

Optionally: Group By - for summ or mean values:可选: Group By - 求和或平均值:

f2 = melted.groupby(['Column']).sum()
df2

在此处输入图像描述

在此处输入图像描述

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

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