用熊猫交叉搜索两个数据框

时间:2021-06-29 22:53:38

标签: python pandas

我遇到了一个我以前没有见过的问题。 情况如下 我有 2 个彼此不同的数据框:

  • cc_etn 有退票票
  • etn 有购买过的用户信息

我想通过id查询退款用户的信息。我所做的是在 cc_etn 中创建一个列表,然后使用方法 'df.loc' 遍历 etn 以找到它们,但是我得到一个空的 DataFrame。 这里有趣的是,要在 etn 中找到用户,有 4 列必须与我在 cc_etn 中的列相等。现在,如果我将 4 个条件添加到 df.loc 中,我会得到空的 DataFrame,并且如果我在获得我想要的值但仍然缺少其他 3 个必须等于找到用户的列时添加一个条件,我也试图制作一个 df.loc 链来找到它们,但我也得到了空,所以我的代码看起来像这样:

cc_etn = pd.read_csv(CONTRA CARGOS APLICADOS .csv',dtype='str')
etn = pd.read_csv(query_result_2021-06-29T15_41_38.794753Z.csv')

#here I list the columns that are relevant to finding the user in etn.
fecha = list(cc_etn['Fecha de Transacción'])
tarjeta = list(cc_etn['Número de Tarjeta'])
monto_1 = list(cc_etn['Monto Publicado'])
codigo = list(cc_etn['Código de \nAutenticación']) 

#here I cycle through to find each user but they all come up empty
for i in range(len(monto):
  (duplicados.loc[(duplicados['created_at_day'] == fecha[i]) & 
  (duplicados['card_number']==tarjeta[i]) & (duplicados['amount'] == monto[i]) & 
  (duplicados['card_authorization']==codigo[i])]

#now if i print this if it works but i don't get what i want
duplicados.loc[duplicados['created_at_day'] == fecha[1]
duplicados.loc[duplicados['card_number']==tarjeta[1]
duplicados.loc[duplicados['amount'] == monto[1]
duplicados.loc[duplicados['card_authorization']==codigo[1]

#and if I do this i have again a empty DataFrame
x = duplicados.loc[duplicados['created_at_day'] == fecha[1]
y = x.loc[x['card_number']==tarjeta[1]

我也有这个错误:

#SettingWithCopyWarning: A value is trying to be set on a copy of a 
#slice from a DataFrame

0 个答案:

没有答案