简体   繁体   English

如何使用 colmun 名称旋转 pandas.dataframe

[英]How can I rotate a pandas.dataframe using colmun names

I have the following DataFrame, with Timestamp as the index:我有以下DataFrame,以Timestamp为索引:

Timestamp 40_x 40_y 40_z 60_x 60_y 60_z 80_x 80_y 80_z
    10:00    a    b    c    d    e    f    g    h    i
    10:10   a2   b2   c2   d2   e2   f2   g2   h2   i2

I want to rotate it into the following DataFrame:我想把它旋转成下面的DataFrame:

                  x   y   z
Timestamp range               
10:00        40   a   b   c
10:00        60   d   e   f
10:00        80   g   h   i
10:10        40  a2  b2  c2
10:10        60  d2  e2  f2
10:10        80  g2  h2  i2

I have a list of ranges: [40, 60, 80] , and it can contain many more values.我有一个范围列表: [40, 60, 80] ,它可以包含更多值。

How about a MultiIndex then stack ?那么MultiIndex那么stack怎么样?

df.columns = df.columns.str.split('_',expand=True)

print(df)

           40          60          80        
            x   y   z   x   y   z   x   y   z
Timestamp                                    
10:00       a   b   c   d   e   f   g   h   i
10:10      a2  b2  c2  d2  e2  f2  g2  h2  i2

df1 = df.stack(0)

print(df1)

               x   y   z
Timestamp               
10:00     40   a   b   c
          60   d   e   f
          80   g   h   i
10:10     40  a2  b2  c2
          60  d2  e2  f2
          80  g2  h2  i2

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

相关问题 如何将pandas.dataframe的索引提高四分之一? - How can I advance the index of a pandas.dataframe by one quarter? 如何使用 pandas.dataframe 转换表格? - How to convert a table using pandas.dataframe? 如何根据`pandas.DataFrame.index`计算两个`pandas.DataFrame`的总和? - How can I calculate the sum of two `pandas.DataFrame` based on `pandas.DataFrame.index`? 从数组转换为pandas.dataframe时如何在数据框中仅添加一行 - How can i add just one row in dataframe when I convert from array to pandas.dataframe 如何使用另一个pandas.DataFrame中的数据填充pandas.DataFrame中的列? - How to fill column in pandas.DataFrame using data from another pandas.DataFrame? 如何在 pandas.DataFrame 上将“.”替换为“,” - How To Replace " . " With " , " on a pandas.DataFrame 我如何在 pycharm 中查看完整的 pandas.dataframe 列而不是“...”? - How i can look full pandas.dataframe columns in pycharm intstead “…”? 如何将 pandas.Dataframe 添加到创建为 test_data 的文件中? - How can I add pandas.Dataframe to file that is created as test_data? 如何在 pandas.DataFrame 中找到值的位置? - How do I find the position of a value in a pandas.DataFrame? 如何使用对齐的空格chacarcters将python pandas.DataFrame写入文件? - How do I write a python pandas.DataFrame to a file using aligned space chacarcters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM