Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

python - ValueError: Can only compare identically-labeled Series objects in pandas?

[Sample Code]

d = {
   'country': ['IN', 'USA', 'USA', 'IN'],
   'username': ['abi.g', 'pugal.g', 'jan.g', 'jacob.h'],
   'email': ['[email protected]', '[email protected]', '[email protected]', '[email protected]'],
   'ClusterID': ['', '4', '5', '9']
}
df1 = pd.DataFrame(d)

data = [
   ['USA', 3490.89, 'qcx_taskid85_duns250437449', '3'], 
   ['JA', 1211, 'Pugal Gandi', '4'], 
   ['USA', 3455.00, 'Janani Khannan', '6']
]
df2 = pd.DataFrame(data, columns=['country', 'salary', 'name', 'ClusterID'])

df1.reset_index(inplace=True, drop=True)
df2.reset_index(inplace=True, drop=True)
df1.loc[df1['ClusterID'] == df2['ClusterID']]

enter image description here enter image description here

Qns: How to compare/filter the two columns using .loc, while the no. of records are different in pandas?

Thanks,

question from:https://stackoverflow.com/questions/65640759/valueerror-can-only-compare-identically-labeled-series-objects-in-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this >>

df1.loc[df1['ClusterID'].isin(df2['ClusterID'])]

Both df1['ClusterID'] & df2['ClusterID'] are series. Equating two series within loc method will throw the exception. Instead, calling isin method will give bool outputs and hence for all True values you will get the corresponding output with loc method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...