[英]How to append subset data-frame to another empty data-frame in for loop python
I'm having one empty data-frame and a list of columns in list1.我有一个空数据框和 list1 中的列列表。 I want append the subset data-frame to empty in a for loop.
我希望 append 子集数据帧在 for 循环中清空。
df
A1 A2 B1 B2
0 1 11 21 31
1 2 12 22 32
2 3 13 23 33
3 4 14 24 34
empty_df = pd.DataFrame()
listl = [['A1','A2'],['B1','B2']]
for columns in list1:
empty_df = empty_df.append(df[columns])
Here when I executed above code I'm getting shape(16X4), where I should get shape(16X1) Which should look like.在这里,当我执行上面的代码时,我得到了 shape(16X4),我应该得到 shape(16X1),它应该看起来像。
df
0 1 11
1 2 12
2 3 13
3 4 14
4 21 31
5 22 32
6 23 33
7 24 34
Also is it possible to add another column which describes previous column names like...还可以添加另一列来描述以前的列名,例如...
df df
0 1 11 A
1 2 12 A
2 3 13 A
3 4 14 A
4 21 31 B
5 22 32 B
6 23 33 B
7 24 34 B
listl = [['A1','A2'],['B1','B2']]
names = ["A", "B"] # or [a_list[0][0] for a_list in listl]
result = pd.DataFrame()
for name, cols in zip(names, listl):
df_to_concat = df.loc[:, cols]
df_to_concat.columns = [f"col{j}" for j in range(len(cols))]
df_to_concat["name"] = name
result = pd.concat([result, df_to_concat]).reset_index(drop=True)
First we get the df to concatanate, then rename its columns to ease the concatanatetion process, then put a name
column to it to signal where it came from and lastly concatanate with what we already have in result
(and reset the index to have a 0..N index).首先,我们让 df 进行连接,然后重命名它的列以简化连接过程,然后给它添加一个
name
列来指示它来自哪里,最后与我们已经拥有的result
连接(并将索引重置为 0 ..N 指数)。
>>> result
col0 col1 name
0 1 11 A
1 2 12 A
2 3 13 A
3 4 14 A
4 21 31 B
5 22 32 B
6 23 33 B
7 24 34 B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.