我已经看到与question和answer有关的用户警告:布尔系列键将被重新索引以匹配DataFrame索引
我尽了最大的努力尝试单独分解命令,但仍然得到UserWarning。我该怎么做才能使下面的内容更加明确,并消除最后两个语句(dfa_ = dfa[qux]
和dfb_ = dfb[qux]
)的警告?
import random
import pandas as pd
fields = ['foo', 'bar', 'baz']
def randstring(x=3):
return ''.join([chr(random.randrange(65, 91)) for _ in range(x)])
def randstrlist(x=3):
return [randstring(x) for _ in range(5)]
values = {field: randstrlist() for field in fields}
def getitem():
return {field: random.choice(values[field]) for field in fields}
def getdata(setsize=100):
return [getitem() for _ in range(setsize)]
dfa = getdata()
dfb = getdata()
cols = dfa.columns.tolist()
dfa['qux'] = dfa.groupby(cols).cumcount()
dfb['qux'] = dfb.groupby(cols).cumcount()
cols = cols + ['qux']
dfo = pd.merge(dfa, dfb, on=cols, how='outer').drop('qux', 1)
qux = dfo.isnull().any(axis=1)
dfa_ = dfa[qux]
dfb_ = dfb[qux]