Python 如何在非';左数据帧中的t
我有两个数据帧,我试图输出一个数据帧中的数据,而不是另一个 我可以在第一个数据帧中获取数据,但不能在第二个数据帧中使用Python 如何在非';左数据帧中的t,python,pandas,Python,Pandas,我有两个数据帧,我试图输出一个数据帧中的数据,而不是另一个 我可以在第一个数据帧中获取数据,但不能在第二个数据帧中使用 only_new = old.merge( new, 'outer', on=['Employee ID', 'Benefit Plan Type'], suffixes=['','_'], indicator=True ).query('_merge == "left_only"').reindex_axis(old.columns, axis=1) 下面是我用来获取仅在第
only_new = old.merge(
new, 'outer', on=['Employee ID', 'Benefit Plan Type'],
suffixes=['','_'], indicator=True
).query('_merge == "left_only"').reindex_axis(old.columns, axis=1)
下面是我用来获取仅在第二个数据帧中的数据的方法
only_new =new.merge(
old, 'outer', on=['Employee ID', 'Benefit Plan Type'],
suffixes=['','_'], indicator=True
).query('_merge == "left only"').reindex_axis(new.columns, axis=1)
但是它不返回任何数据,但是使用Excel我可以看到应该有几行
看来这应该行得通
only_new = old.merge(new, on='Employee ID', indicator=True, how='outer',
only_new[only_new['_merge'] == 'right_only'])
但我明白了
SyntaxError: non-keyword arg after keyword arg
似乎您需要将
“\u merge==“left\u only”
更改为”\u merge==“right\u only”
考虑数据帧旧的和新的
old = pd.DataFrame(dict(
ID=[1, 2, 3, 4, 5],
Type=list('AAABB'),
Total=[9 for _ in range(5)],
ArbitraryColumn=['blah' for _ in range(5)]
))
new = pd.DataFrame(dict(
ID=[3, 4, 5, 6, 7],
Type=list('ABBCC'),
Total=[9 for _ in range(5)],
ArbitraryColumn=['blah' for _ in range(5)]
))
然后取对称相同的解
old.merge(
new, 'outer', on=['ID', 'Type'],
suffixes=['_', ''], indicator=True # changed order of suffixes
).query('_merge == "right_only"').reindex_axis(new.columns, axis=1)
# \......../ \./
# changed from `left` to `right` reindex with `new`
ArbitraryColumn ID Total Type
5 blah 6 9.0 C
6 blah 7 9.0 C
这很有效。当我输出它时,我只得到我要连接的两列,第一列得到所有列。这是来自代码中的其他地方吗?嗯,看来主要问题出在reindex\u轴上。如果删除它,则会得到相同的输出,因为reindex\u axis
会过滤掉数据,这些数据不在old.columns
或new.columns
中,在靠近输出时,它会返回第一个数据帧中的数据,但只返回ID和计划类型列,这是上半部分正确执行的操作。我在摆弄它,想换个方向。