简体   繁体   English

每第 n 列重塑 dataframe

[英]Reshaping a dataframe every nth column

I have two datasets.我有两个数据集。 After merging them horzontally, and sorting the columns with the following code, I get the dataset below:水平合并它们并使用以下代码对列进行排序后,我得到以下数据集:

df= df=

X X Y
5.2 5.2 6.5 6.5
3.3 3.3 7.6 7.6

df_year= df_year=

X X Y
2014 2014 2014 2014
2015 2015年 2015 2015年
df_all_cols = pd.concat([df, df_year], axis = 1)
sorted_columns = sorted(df_all_cols.columns)
df_all_cols_sort = df_all_cols[sorted_columns]
X X X X Y Y
5.2 5.2 2014 2014 6.5 6.5 2014 2014
3.3 3.3 2015 2015年 7.6 7.6 2015 2015年

I am trying to make my data look like this, by stacking the dataset every 2 columns.我试图通过每 2 列堆叠数据集来使我的数据看起来像这样。

name名称 year Variable多变的
5.2 5.2 2014 2014 X X
3.3 3.3 2015 2015年 X X
6.5 6.5 2014 2014 Y
7.6 7.6 2015 2015年 Y

One approach could be as follows:一种方法如下:

  • Apply df.stack to both dfs before feeding them to pd.concat .在将它们提供给pd.concat之前将df.stack应用于两个dfs The result at this stage being:这个阶段的结果是:
       0     1
0 X  5.2  2014
  Y  6.5  2014
1 X  3.3  2015
  Y  7.6  2015
  • Next, use df.sort_index to sort on the original column names (ie "X, Y" , now appearing as index level 1 ), and get rid of index level 0 ( df.droplevel ).接下来,使用df.sort_index对原始列名(即"X, Y" ,现在显示为index level 1 )进行排序,并删除index level 0 ( df.droplevel )。
  • Finally, use df.reset_index with drop=False to insert index as a column and rename all the columns with df.rename .最后,使用df.reset_indexdrop=False将索引作为列插入,并使用df.rename重命名所有列。
res = (pd.concat([df.stack(),df_year.stack()], axis=1)
       .sort_index(level=1)
       .droplevel(0)
       .reset_index(drop=False)
       .rename(columns={'index':'Variable',0:'name',1:'year'})
       )

# change the order of cols
res = res.iloc[:, [1,2,0]]

print(res)

   name  year Variable
0   5.2  2014        X
1   3.3  2015        X
2   6.5  2014        Y
3   7.6  2015        Y

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

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