gpt4 book ai didi

python - 根据特定规则将 Pandas 数据框索引拆分为列

转载 作者:行者123 更新时间:2023-12-01 01:49:57 24 4
gpt4 key购买 nike

如何使用 pandas 进行编码来转换这个名为 df1 的数据框

index,client1,client2
name,bob,erika
email,gmail,yahoo
house_A,Paris,London
house_B,London,Milan
house_C,Berlin,Paris
code_name_A,Vaugirard,Windsor
code_name_B,Great,Brera
code_name_C,Mauer,Elysee
visa_id_num_A,FR001B,UK001E
visa_id_num_B,UK001B,IT001E
visa_id_num_C,GE001B,FR001E
food_A,apples,burgers
food_B,bananas,fries
food_C,burgers,pizzas
food_D,fries,oranges
food_E,pizzas,pears

到这个名为 df2 的数据框

 index,FR001B,UK001B,GE001B,UK001E,IT001E,FR001E
client_number,client1,client1,client1,client2,client2,client2
name,bob,bob,bob,erika,erika,erika
email,gmail,gmail,gmail,yahoo,yahoo,yahoo
house,Paris,London,Berlin,London,Milan,Paris
code_name,Vaugirard,Great,Mauer,Windsor,Brera,Elysee
visa_id_num,FR001B,UK001B,GE001B,UK001E,IT001E,FR001E
food_A,apples,apples,apples,burgers,burgers,burgers
food_B,bananas,bananas,bananas,fries,fries,fries
food_C,burgers,burgers,burgers,pizzas,pizzas,pizzas
food_D,fries,fries,fries,oranges,oranges,oranges
food_E,pizzas,pizzas,pizzas,pears,pears,pears

我需要拆分索引值并用新值替换特定索引值。我尝试过 stack、unstack 和 groupby,但很困惑。

提前非常感谢

最佳答案

让我们尝试一下,使用 Tpd.wide_to_long 来处理多个“melt”,以及 set_index:

df1T = df1.T.reset_index().rename(columns={'index':'client_number'})
df1w = pd.wide_to_long(df1T,
['house','code_name','visa_id_num'],
['client_number','name','email',
'food_A','food_B',
'food_C','food_D','food_E'],
'code', '_', '\w+')

df2 = df1w.reset_index().set_index('visa_id_num').T
print(df2)

输出:

visa_id_num       FR001B   UK001B   GE001B   UK001E   IT001E   FR001E
client_number client1 client1 client1 client2 client2 client2
name bob bob bob erika erika erika
email gmail gmail gmail yahoo yahoo yahoo
food_A apples apples apples burgers burgers burgers
food_B bananas bananas bananas fries fries fries
food_C burgers burgers burgers pizzas pizzas pizzas
food_D fries fries fries oranges oranges oranges
food_E pizzas pizzas pizzas pears pears pears
code A B C A B C
house Paris London Berlin London Milan Paris
code_name Vaugirard Great Mauer Windsor Brera Elysee

关于python - 根据特定规则将 Pandas 数据框索引拆分为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com