[英]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 ![]() |
Y![]() |
---|---|
5.2 ![]() |
6.5 ![]() |
3.3 ![]() |
7.6 ![]() |
df_year= df_year=
X ![]() |
Y![]() |
---|---|
2014 ![]() |
2014 ![]() |
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 ![]() |
Y![]() |
Y![]() |
---|---|---|---|
5.2 ![]() |
2014 ![]() |
6.5 ![]() |
2014 ![]() |
3.3 ![]() |
2015 ![]() |
7.6 ![]() |
2015 ![]() |
I am trying to make my data look like this, by stacking the dataset every 2 columns.我试图通过每 2 列堆叠数据集来使我的数据看起来像这样。
name![]() |
year![]() |
Variable![]() |
---|---|---|
5.2 ![]() |
2014 ![]() |
X ![]() |
3.3 ![]() |
2015 ![]() |
X ![]() |
6.5 ![]() |
2014 ![]() |
Y![]() |
7.6 ![]() |
2015 ![]() |
Y![]() |
One approach could be as follows:一种方法如下:
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
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
)。df.reset_index
with drop=False
to insert index as a column and rename all the columns with df.rename
.df.reset_index
和drop=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.