【问题标题】:pandas isin function on a for loopfor 循环上的 pandas isin 函数
【发布时间】:2020-07-13 11:23:01
【问题描述】:

1.csv

     cut  price  depth  carat  table
0   Good    327   57.9   0.23   65.0
1   Good    335   63.3   0.31   58.0
2 Very Good 336   62.8   0.24   57.0
3 Very Good 336   62.3   0.24   57.0
4 Very Good 337   61.9   0.26   55.0
5 Premium   326   59.8   0.21   61.0
6  Premium  334   62.4   0.29   58.0
7   Good    400   64.0   0.30   55.0

2.csv

     cut  price  depth  carat  table
0   Good    327   57.9   0.23   65.0
1   Good    335   63.3   0.31   58.0
2 Very Good 336   62.8   0.24   57.0
3 Very Good 336   62.3   0.24   57.0
4 Very Good 337   61.9   0.26   50.0
5 Premium   326   59.8   0.21   61.0
6  Premium  334   60.4   0.29   58.0
7   Good    399   64.0   0.30   55.0

2.csv 中只有 4、6、7 行发生了变化

我正在寻找

这样输出

     cut  price  depth  carat  table
4 Very Good 337   61.9   0.26   50.0
6  Premium  334   60.4   0.29   58.0
7   Good    399   64.0   0.30   55.0

任何人都可以分享您的经验任何形式的帮助都可以

import pandas as pd
f1 = pd.read_csv('1.csv')
f2 = pd.read_csv('2.csv')
columns_list = ['cut', 'price', 'depth', 'carat', 'table']

new_df= f2[~f2.price.isin(f1.price)]
print(new_df)

这是我编写的示例代码,它工作正常,但我需要使用

f2[~f2.price.isin(f1.price)]

在一个循环中获取该“价格”空间上的每个列名称,这也将返回该值。我以这样的正常方式尝试过

for i in columns_list:
price = f2[~f2.i.isin(f1.i)]
print(price)

但是 pandas 命令不能像这样使用它会返回类似的错误

AttributeError: 'DataFrame' object has no attribute 'i'

感谢阅读,希望你能理解

【问题讨论】:

  • 您好,您可以尝试以下方法:price = f2[~f2[i].isin(f1[i])]。这个命名法应该有效。实际字符串不是数据框的属性。因此,您必须使用其他方法选择列。让我知道这是否有帮助。

标签: python pandas dataframe for-loop isin


【解决方案1】:

IIUC,DataFrame.mergeindicator = True

f2_filtered = (f2.merge(f1, how='outer', indicator=True)
                 .query('_merge == "left_only"')
                 .drop(columns = '_merge'))
print(f2_filtered)

输出

         cut  price  depth  carat  table
4  Very_Good    337   61.9   0.26   50.0
6    Premium    334   60.4   0.29   58.0
7       Good    399   64.0   0.30   55.0

【讨论】:

  • 实际上它正在使用合并函数,但它得到了正确的输出,非常感谢兄弟
猜你喜欢
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
相关资源
最近更新 更多