[英]How to append a dataframe row to another within a for loop using .loc?
Let's say I have the following dataframes: 假设我有以下数据框:
df_t1 = pd.DataFrame([["AAA", 1 ,2],["BBB", 0, 3],["CCC", 1, 2],["DDD", 0, 0],["EEE", 0, 3]], columns=list('ABC'))
A B C
0 AAA 1 2
1 BBB 0 3
2 CCC 1 2
3 DDD 0 0
4 EEE 0 3
and 和
df_t2 = pd.DataFrame([["XXX", 4, 1],["YYY", 5 ,6],["ZZZ", 0, 3]], columns=list('ABC'))
A B C
0 XXX 4 1
1 YYY 5 6
2 ZZZ 0 3
I can locate the rows in df_t1 that meet a certain condition using the code below: 我可以使用以下代码在df_t1中找到满足特定条件的行:
df_t1.loc[(df_t1['B'] <= 2) & (df_t1['C'] > 2)]
A B C
1 BBB 0 3
4 EEE 0 3
df_t1.loc[(df_t1['B'] <= 3) & (df_t1['C'] > 3)]
A B C
[Empty Dataframe]
I can create a for loop that returns those same results: 我可以创建一个for循环,返回相同的结果:
for i in value_check:
print(df_t1.loc[(df_t1['B'] <= i) & (df_t1['C'] > i)])
A B C
1 BBB 0 3
4 EEE 0 3
Empty DataFrame
Columns: [A, B, C]
Index: []
But when I try to use that code to attach those values to df_t2: 但是,当我尝试使用该代码将这些值附加到df_t2时:
value_check = [2,3]
for i in value_check:
df_t2.append(df_t1.loc[(df_t1['B'] <= i) & (df_t1['C'] > i)])
df_t2 is unchanged df_t2不变
From the docs, the append method: "Appends rows of other to the end of this frame, returning a new object". 从文档中,添加方法:“将其他行追加到该帧的末尾,返回一个新对象”。 You have to use assign
df_t2
in your loop: 您必须在循环中使用assign
df_t2
:
value_check = [2,3]
for i in value_check:
df_t2 = df_t2.append(df_t1.loc[(df_t1['B'] <= i) & (df_t1['C'] > i)])
That said, concatenating is more efficient. 也就是说,连接更为有效。
Edit: Here is a implementation using concatenation and list comprehension 编辑:这是使用串联和列表理解的实现
df_t2 = pd.concat([df_t2] + [df_t1.loc[(df_t1['B'] <= i) & (df_t1['C'] > i)] for i in value_check], axis=0)
Using pd.concat
使用
pd.concat
df_t1 = pd.DataFrame([["AAA", 1 ,2],["BBB", 0, 3],["CCC", 1, 2],["DDD", 0, 0],["EEE", 0, 3]], columns=list('ABC'))
df_t2 = pd.DataFrame([["XXX", 4, 1],["YYY", 5 ,6],["ZZZ", 0, 3]], columns=list('ABC'))
value_check = [2, 3]
for i in value_check:
condition = (df_t1['B'] <= i) & (df_t1['C'] > i)
row_to_add = df_t1.loc[condition]
df_t2 = pd.concat([df_t2, row_to_add], axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.