我有以下数据框ds
,它是通过.merge
得出的:
Date_x Invoice_x Name Coupon_x Location_x Date_y \
1 2017-12-24 700349.0 John Doe NONE VAGG1 2017-12-24
2 2017-12-24 700349.0 John Doe NONE VAGG1 2017-12-24
4 NaN NaN Sue Simpson NaN NaN 2017-12-23
Invoice_y Price Coupon_y Location_y
1 800345 17.95 CHANGE VAGG1
2 800342 9.95 GADSLR VAGG1
4 800329 34.95 GADSLR GG2
我正在寻找的是:
的输出 Date Invoice Name Coupon Location Price
1 2017-12-24 700349 John Doe NONE VAGG1 17.95
2 2017-12-24 700349 John Doe NONE VAGG1 9.95
使用以下代码:
ds = ds.query('Price_x != Price_y')
我得到了
Date_x Invoice_x Name Price_x Coupon_x Location_x \
1 2017-12-24 700349.0 John Doe 59.95 NONE VAGG1
2 2017-12-24 700349.0 John Doe 59.95 NONE VAGG1
4 NaN NaN Sue Simpson NaN NaN NaN
Date_y Invoice_y Price_y Coupon_y Location_y
1 2017-12-24 800345 17.95 CHANGE VAGG1
2 2017-12-24 800342 9.95 GADSLR VAGG1
4 2017-12-23 800329 34.95 GADSLR GG2
哪个接近我想要的。可以通过.drop
和.rename
删除额外的列。真正缺少的是能够摆脱名称只出现一行的行。
我一直在查询语句中沿着以下几行尝试逻辑:
ds =ds.query('Price_x != Price_y & Name > 1')
导致以下错误:
TypeError: '>' not supported between instances of 'str' and 'int'
编辑:
ds = ds[(ds[Price_x] != ds[Price_y]) & (ds['Name'].value_counts() > 1)]
结果是:
NameError: name 'Price_x' is not defined
或者,尝试:
ds = ds[(ds.Price_x != ds.Price_y) & (ds['Name'].value_counts() > 1)]
结果
c:\users\...\python\python36\lib\site-packages\pandas\core\indexes\base.py:3140: RuntimeWarning: '<' not supported between instances of 'int' and 'str', sort order is undefined for incomparable objects
return this.join(other, how=how, return_indexers=return_indexers)
C:\Users\...\Python\Python36\Scripts\ipython:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
以及ds
为空。
Empty DataFrame
Columns: [Date_x, Invoice_x, Name, Price_x, Coupon_x, Location_x, Date_y, Invoice_y, Price_y, Coupon_y, Location_y]
Index: []
答案 0 :(得分:1)
试试这个
ds = ds[ds.groupby('Name').Name.transform(len) > 1]
ds = ds.query('Price_x != Price_y')
第一行删除仅出现一次的名称。有关详细信息,请参阅此Remove values that appear only once in a DataFrame column。
此外,在你的错误df [Price_x] - &gt;它应该是df [&#34; Price_x&#34;]。一个人可以做df.Price_x或df [&#34; Price_x&#34;]。
答案 1 :(得分:1)
您可以通过多个步骤执行此操作:首先使用pd.value_counts
计算每个名称的出现次数,然后将其连接到原始数据并对其进行查询。例如:
counts = pd.value_counts(ds.Name).reset_index()
counts.columns = ['Name', 'Name_count']
ds.merge(counts, on='Name').query('Price_x != Price_y & Name_count > 1')