[英]Transposing rows into columns in python
I have Fila_ID and MIHF_ID and Total. 我有Fila_ID和MIHF_ID以及总计。 I need to tranpose MIHF_ID into columns based on total.
我需要根据总数将MIHF_ID转换为列。 Each Filaa_ID has multiple MIHF_ID.
每个Filaa_ID具有多个MIHF_ID。 I tried with Pivot but that does not help me further for clustering.
我尝试过使用Pivot,但这并不能进一步帮助我进行群集。 I need the total column to be present as well.
我也需要总列。
FILA_ID MIHF_ID Total
0 1514 34338 249525.220
1 1484 34338 240921.760
2 1514 30927 222260.790
3 1484 30929 214958.440
4 10481 34338 209155.460
... ... ... ...
289783 10070 973713 0.000
289784 422 973713 0.000
289785 312 31563 0.000
289786 556 973713 0.000
289787 29 973713 0.000
I already tried using group by and unstacking but then I am unable to select the transformed columns. 我已经尝试过使用group by和unstack,但是后来我无法选择转换后的列。
df_ = df.groupby(['FILA_ID','MIHF_ID'])['Total'].sum().unstack(fill_value=0)
I expect to have Fila_ID and MIHF_ID and total as columns. 我希望将Fila_ID和MIHF_ID总计作为列。
If I understand your question correctly, something like this? 如果我正确理解您的问题,是这样的吗?
import pandas as pd
df = pd.DataFrame({"FILA_ID": [1514, 1484, 1514, 1484, 10481],
"MIHF_ID": [34338, 34338, 30927, 30929, 34338],
"Total": [249525.220, 240921.760, 222260.790, 214958.440, 209155.460]})
df_new = pd.DataFrame(df.groupby(['FILA_ID','MIHF_ID'])['Total'].sum().unstack(fill_value=0).stack())
df_new.reset_index(inplace=True)
df_new.rename(columns = {0:'Total'}, inplace=True)
df_new
FILA_ID MIHF_ID Total
0 1484 30927 0.00
1 1484 30929 214958.44
2 1484 34338 240921.76
3 1514 30927 222260.79
4 1514 30929 0.00
5 1514 34338 249525.22
6 10481 30927 0.00
7 10481 30929 0.00
8 10481 34338 209155.46
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.