简体   繁体   English

Pandas 为每一列添加行

[英]Pandas Add rows for each column

I apologise for the title, I know it isn't the most helpful.我为标题道歉,我知道这不是最有帮助的。 What I'm attempting to do is restructure my data so that each of a given column is given it's own row with certain values carried over from the previous dataframe.我正在尝试做的是重组我的数据,以便给定的每一列都有它自己的行,其中某些值从前一个数据帧中继承。

My Data in its current form is something like this:我当前形式的数据是这样的:

ColA | ColB | ColC | val1 | val2 | val3
   1 |    2 |    3 | A    | B    | C
   4 |    5 |    6 | D    | E    | F

And I want to restructure it so I get a result like this:我想重组它,所以我得到这样的结果:

 ColA | ColB | ColC | val
    1 |    2 |    3 | A
    1 |    2 |    3 | B
    1 |    2 |    3 | C
    4 |    5 |    6 | D 
    4 |    5 |    6 | E 
    4 |    5 |    6 | F 

How would I do this?我该怎么做?

I know I could go through each row, grab the relevant data and concat a dataframe but I was hoping for a much better alternative我知道我可以遍历每一行,获取相关数据并连接一个数据框,但我希望有一个更好的选择

Given:鉴于:

   ColA  ColB  ColC val1 val2 val3
0     1     2     3    A    B    C
1     4     5     6    D    E    F

Doing:正在做:

df.melt(['ColA', 'ColB', 'ColC'])

Output:输出:

   ColA  ColB  ColC variable value
0     1     2     3     val1     A
1     4     5     6     val1     D
2     1     2     3     val2     B
3     4     5     6     val2     E
4     1     2     3     val3     C
5     4     5     6     val3     F

I think the following code should solve your problem.我认为以下代码应该可以解决您的问题。 I created an example with the sample data you provided.我使用您提供的示例数据创建了一个示例。 The code involves stacking and merging the dataframes and columns.该代码涉及堆叠和合并数据框和列。

df_start = pd.DataFrame()

df_start = df_start.append([[1, 2, 3, "A", "B", "C"], [4, 5, 6, "D", "E", "F"]])
df_start = df_start.rename(columns={0: "ColA", 1: "ColB", 2: "ColC", 3: "val1", 4: "val2", 5: "val3"})
df_start["vals"] = df_start.values.tolist()
df_start["vals"] = df_start["vals"].apply(lambda x: x[3:])

df_ = df_start["vals"].apply(pd.Series).stack().reset_index(level=1, drop=True).to_frame('val')
result_df = pd.merge(df_, df_start, left_index=True, right_index=True)

result_df = result_df.iloc[:, [1, 2, 3, 0]]
result_df

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

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