简体   繁体   English

熊猫 dataframe 有效地将列和 append 作为新行

[英]Panda dataframe take column and append as new rows efficiently

If I have a df:如果我有一个df:

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])

and wish to take the second column "b" and append to the end of a "new" df with the columns "a" and "b" and a name column containing the name of the "b" column and then the third column "c" and append to the end of the new df together with "a" and the name "c" appened to the name column.并希望将第二列“b”和 append 放在“新”df 的末尾,其中包含“a”和“b”列,名称列包含“b”列的名称,然后是第三列“ c" 和 append 到新 df 的末尾连同 "a" 和名称 "c" 附加到名称列。 It is timeseries data with a datetime in "a" and a variable in b and c and there is sometimes 20 variables and sometimes 1 or 2.它是时间序列数据,日期时间在“a”中,变量在 b 和 c 中,有时有 20 个变量,有时有 1 个或 2 个。

How do I do that in a pretty and efficient way.我如何以一种漂亮而有效的方式做到这一点。 right now im doing it like this but have to do it a 100 times for slightly different df but with the same idea.现在我正在这样做,但必须为略有不同的 df 但具有相同的想法做 100 次。

col_nam_list = list(df.columns.values)
df_1 = pd.DataFrame()
df_1["a"] = df["a"]
df_1["name"] = col_nam_list[1]
df_1["value"] = df["b"]

df_2 = pd.DataFrame()
df_2["a"] = df["a"]
df_2["name"] = col_nam_list[2]
df_2["value"] = df["c"]

result = pd.concat([df_1, df_2])

This should be the output这应该是 output 结果

Now this is not fun to write and looks so ugly and unnecessary long.现在这写起来并不有趣,而且看起来很丑陋和不必要的长。 How do I improve my method?如何改进我的方法?

BR BR

IIUC, you can use pd.DataFrame.melt with parameter id_vars equal to 'a', IIUC,您可以使用参数pd.DataFrame.melt等于“a”的id_vars

df.melt('a')

Output: Output:

   a variable  value
0  1        b      2
1  4        b      5
2  7        b      8
3  1        c      3
4  4        c      6
5  7        c      9

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

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