简体   繁体   English

pandas 将第一个多索引转换为行索引,将第二个多索引转换为列索引

[英]pandas transform 1st mutliindex to rowindex and 2nd multiindex to columnindex

So I have the following code which transforms groups a given dataframe by to columns and calculates the size of each group.所以我有以下代码将给定的 dataframe 组转换为列并计算每个组的大小。 This creates a DataFrame with one column and a Multiindex of two values.这将创建一个 DataFrame,其中包含一列和两个值的 Multiindex。 Afterwards I transfrom the result to put the 2nd Index as columnindex and the 1st index as rowindex.之后我将结果转换为将第二个索引作为 columnindex 并将第一个索引作为 rowindex。

import pandas as pd
from random import choice

products=['car', 'pen', 'kitchen', 'bed']
df=pd.DataFrame([{'product': choice(products), 'Changing': choice([True, False])} for _ in range(10000)])

df_grp=df.groupby(['product', 'Changing']).size().to_frame()
print(df_grp)
print('-'*50)

d=[]
for i in df_grp.reset_index()['product'].unique():
    d.append({'product':i, 'not changing': df_grp.loc[(i,False)][0], 'changing': df_grp.loc[(i,True)][0]})
print(pd.DataFrame(d).set_index('product'))

this results in the following output这导致以下 output

                     0
product Changing      
bed     False     1253
        True      1269
car     False     1244
        True      1275
kitchen False     1236
        True      1271
pen     False     1206
        True      1246
--------------------------------------------------
         not changing  changing
product                        
bed              1253      1269
car              1244      1275
kitchen          1236      1271
pen              1206      1246

this is exactly what I want to achieve.这正是我想要实现的。 However, my approch seems a bit complicated.但是,我的方法似乎有点复杂。 Is there a more elegant way to do this?有没有更优雅的方法来做到这一点? Maybe with da built-in pandas function or a lambda function?也许与 da 内置 pandas function 或 lambda ZC1C425268E68385D1AB50A? Also my approach does not generalize well since I always assume that the second index is a bool.我的方法也不能很好地概括,因为我总是假设第二个索引是布尔值。

try via unstack() :通过unstack()尝试:

df_grp=df.groupby(['product', 'Changing']).size().unstack()

Finally make use of columns attribute:最后使用columns属性:

df_grp.columns=[f'{"not "*col}{df_grp.columns.name}' for col in df_grp][::-1]
#as suggested by @Cyttorak #rename column without hard coding
#OR
df_grp.columns=['not changing','changing'] #you have to manually write the names

output of df_grp : df_grp 的df_grp

          not changing  changing
product         
bed         1210        1226
car         1220        1272
kitchen     1238        1277
pen         1267        1290

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

相关问题 Pandas MultiIndex:对每个第一个索引使用相同的第二个索引 - Pandas MultiIndex: Using same 2nd index for each 1st index for循环跳过第一和第二元素 - for loop skipping 1st and 2nd element Python Pandas-使用第一数据从第二数据框中获取位置 - Python Pandas - Get Location from 2nd dataframe using 1st data Pandas 增加一列表示第1和第2位,根据行值 - Pandas to add a column to indicate the 1st and 2nd places, according to row values Pandas 定制 function 查找是否是第一个,第二个等星期一,星期二等 - 欢迎所有建议 - Pandas custom function to find whether it is the 1st, 2nd etc Monday, Tuesday, etc - all suggestions welcome 使用pandas连接2个文本文件,第1个文本文件插入标题,第2个文本文件作为正文 - Joining 2 text files using pandas, 1st text file into header, the 2nd as the body 如何强制 pandas dataframe 的第 2 级加起来达到第 1 级? - How to enforce 2nd level of pandas dataframe to add up to 1st level? Python:MQTT,第二个进程不能干扰第一个进程 - Python: MQTT, 2nd process cannot interfere the 1st process 在pygtk中关闭第一个窗口之前先打开第二个窗口 - Opening 2nd window before closing 1st window in pygtk 编码定义必须在Python的第1/2行中吗? - Must the encoding definition be in the 1st/2nd line in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM