在for循环上的pandas isin函数 [英] pandas isin function on a for loop

查看:111
本文介绍了在for循环上的pandas isin函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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、4、6、7行.csv已更改

only 4,6,7 rows from 2.csv is changed

我希望获得

像这样的输出

     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

任何人都可以分享您的经验吗?

can anyone share your experience any kind of help is fine

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)

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

this is a sample code i wrote and it's working fine but i need to use the

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

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

循环获取该价格空间上的每个列名称,这也将返回该值。

in a loop to get each columns name on that 'price' space and also that will return the value.i tried in normal way like this

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

但是pandas命令是不能像这样工作,它会返回错误,例如

but pandas command is not work with like this way it's return an error like

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

感谢您的阅读,希望您能理解

Thankz for reading, i hope you understand this

推荐答案

IIUC, DataFrame.merge indicator = True

IIUC, DataFrame.merge with indicator = 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

这篇关于在for循环上的pandas isin函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆