【发布时间】:2020-01-18 05:30:18
【问题描述】:
我有两个具有相同结构的数据帧列表,如果 list_a[df_a][col_a] 中的至少一个值存在于任何 list_b 数据帧的 col_a 中,我会尝试忽略 list_a 中的每个数据帧。我已经通过了几次,但还没有找到真正完成它的东西。我的方法可能是错误的,请指出正确的方向!
方法:
for df_a in list_a:
for df_b in list_b:
temp = df_a[~df_a['col_a'].isin([df_b['col_a']])] # error 'list indices must be integers or slices, not
if len(temp.index) > 0:
list_a.remove(df_a)
list_a[0]
col_a temp
877 12/17/2019 0.300807486
886 12/31/2019 0.143508662
list_a[1]
col_a temp
651 7/27/2019 0.435680418
660 8/10/2019 0.229333215
list_b[0]
col_a temp
1 12/31/2019 0.843356517
10 1/14/2020 0.846720719
list_omit[0]
col_a temp
1 12/17/2019 0.600807486
2 12/31/2019 0.143508662
结果: 由于 list_a[0] 和 list_b[0] 的日期重叠为 2019 年 12 月 31 日,因此应将 list_a[0] 从 list_a 中删除并添加到 dfs 的“省略”列表中
转载:
import numpy as np
import pandas as pd
temp = list(range(0, 2))
list_a = []
list_b = []
for l in temp:
df = pd.DataFrame(np.random.randint(0,100,size=(2, 2)), columns=list(['col_a','temp']))
list_a.append(df)
for l in temp:
df = pd.DataFrame(np.random.randint(0,100,size=(2, 2)), columns=list(['col_a','temp']))
list_b.append(df)
print(list_a)
print(list_b)
感谢您的帮助。
【问题讨论】: