简体   繁体   English

将单个数据帧中的多个列合并到单个数据帧中

[英]combine multiple columns in single data frames from single dataframe

I have single data frame which consist of multiple columns name such as "Load_Node_Values,Load_KV_Values,Load_Node_Values,Load_KV_Values,Load_P_ST_Values,Load_ST_Values,G_Node_Values,G_KV_Values,G_P_ST_Values,G_ST_Values,Line_Node_Values,Line_KV_Values,Line_P_ST_Values,Line_ST_Values,T_Node_Values,T_KV_Values,T_P_ST_Values,T_ST_Values,Load_P_ST_Values,Load_ST_Values,G_Node_Values G_KV_Values,G_P_ST_Values,G_ST_Values,Line_Node_Values,Line_KV_Values,Line_P_ST_Values,Line_ST_Values,T_Node_Values,T_KV_Values". 我有包括多个列名单数据帧,如“Load_Node_Values,Load_KV_Values,Load_Node_Values,Load_KV_Values,Load_P_ST_Values,Load_ST_Values,G_Node_Values,G_KV_Values,G_P_ST_Values,G_ST_Values,Line_Node_Values,Line_KV_Values,Line_P_ST_Values,Line_ST_Values,T_Node_Values,T_KV_Values,T_P_ST_Values,T_ST_Values, Load_P_ST_Values,Load_ST_Values,G_Node_Values G_KV_Values,G_P_ST_Values,G_ST_Values,Line_Node_Values,Line_KV_Values,Line_P_ST_Values,Line_ST_Values,T_Node_Values,T_KV_Values”。 All these columns have have numeric as well as string values. 所有这些列都具有数字和字符串值。 I want to combine all the values of Load_Node_values, G_Node_values, Line_Node_values and T_Node_Values into one one single column "new name" and similarly, other column as well into another new column name. 我想将Load_Node_values,G_Node_values,Line_Node_values和T_Node_Values的所有值组合到一个单独的列“新名称”中,同样,将其他列也合并到另一个新列名称中。

这就是数据集的样子

I used frames and put all the columns that want to combine. 我使用框架并将所有要合并的列放进去。 In the code I showed that frame_node, frame_KV, frame_P, frame_ST and put all the columns names in these frames as shown in the code. 在代码中,我显示了frame_node,frame_KV,frame_P,frame_ST,并将所有列名称放在代码中,如代码所示。

frame_KV=[df1['Load_KV_Values'],df2['G_KV_Values'],df3['Line_KV_Values'],df4['T_KV_Values']]
frame_P=[df1['Load_P_ST_Values'],df2['G_P_ST_Values'],df3['Line_P_ST_Values'],df4['T_P_ST_Values']]
frame_ST=[df1['Load_ST_Values'],df2['G_ST_Values'],df3['Line_ST_Values'],df4['T_ST_Values']]

frames=[frame_node,frame_KV,frame_P,frame_ST]

result_nodes=pd.concat(frames)

This is error I got from the text. 这是我从文本中得到的错误。 "TypeError: cannot concatenate object of type ""; only pd.Series, pd.DataFrame, and pd.Panel (deprecated) objs are valid" “ TypeError:无法连接类型为“”的对象;只有pd.Series,pd.DataFrame和pd.Panel(不建议使用)objs有效”

There is list of list of Series , so flatten it for list of Series : Series列表的列表,因此将其展平为Series列表:

result_nodes=pd.concat([y for x in frames for y in x])

Or maybe need first concat by axis=1 fr eacj list and then by default axis=0 (should be omit): 或者,可能需要先通过axis=1 fr eacj list进行连接,然后默认情况下axis=0 (应该省略):

result_nodes=pd.concat(pd.concat([x for x in frames], axis=1))
frame_1=df1[['Load_KV_Values','Load_P_ST_Values','Load_ST_Values']]
frame_2= df2[['G_P_ST_Values','G_KV_Values','G_ST_Values']]
frame_3=df3[['Line_KV_Values','Line_ST_Values','Line_P_ST_Values']
frame_4 = df4[['T_KV_Values','T_P_ST_Values','T_ST_Values']
result_nodes = pd.concat([frame_1, frame_2, frame_3, frame_4], axis =1)

Please check by following method anlso please verify wether all four dataframes have same number of rows. 请通过以下方法进行检查,并请确认所有四个数据框的行数均相同。 --Do upvote -投票

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

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