[英]Set new column values in pandas DataFrame1 where DF2 column values match DF1 index
[英]Match column values and copy row to new df
我正在完成一项我惨遭失败的作业。 我需要遍历一个数据框,根据条件选择行,然后将该行复制到另一个数据框。 尝试使用 df.append(),它似乎可以正常工作,但会掩埋我的机器并为每一行发出弃用警告。 它尝试了 pd.concat() 但语法不正确。 我的错误是它与我不关心的其他列不匹配。
大约有 20k 行,所以它应该花费太长时间。 我很清楚这一点。
是的,我也在使用 iterrows。 如果我需要提供更多细节,请告诉我。
谢谢
KeyError:“[Index([1.0, 'A', '9/1/2004', 'Math', 4, '1'], dtype='object')] 都不在 [columns] 中”
这是我所拥有的:
#get rows that are quantitative and match at least one other row on studentID, classDate and IQ
df_isquant = pd.DataFrame([])
for index, row in df_quant.iterrows():
if row['IQ']== '1':
for yndex, roe in df_quant.iterrows():
if roe['IQ'] == row['IQ'] and roe['StudentID'] == row['StudentID'] and roe['ClassDate'] == row['ClassDate']:
pd.concat(df_isquant[row])
# df_isquant.append(row)
我正在搜索值为“1”的行,如果有,则查看该行是否与“StudentID”、“IQ”和“ClassDate”上的任何其他行匹配。 如果是这样,请复制到另一个数据框。 我也可以简单地创建另一列并使用布尔值来标记符合该描述的行,这可能会使这更容易。 但这给了我足够的悲伤,我现在需要答案。
给定提供的逻辑(“我正在搜索值为 '1' 的行,如果有,则查看该行是否与 'StudentID'、'IQ' 和 'ClassDate' 上的任何其他行匹配。 ”),使用布尔索引和concat
:
# condition on IQ
m1 = df_quant['IQ'].eq('1')
# are there other rows matching the 3 columns
m2 = df_quant[['ID', 'StudentID', 'ClassDate']].duplicated(keep=False)
# concat
df_isquant = pd.concat([df_isquant, df_quant[m1&m2]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.