I am getting an error saying "ValueError: Can only compare identically-labeled Series objects". I got as far as knowing that I am comparing two series objects and not the values inside them. However, I thought series[column]
gave you the value inside. Can someone elaborate more on this? I am truly stuck on where i could find this information and would gradly love to be pointed toward the right direction. all_ages
and recent_grads
are dataframes.
import pandas as pd
majors = recent_grads['Major'].unique()
rg_lower_count = 0
for x in majors:
aa_major = all_ages[all_ages['Major'] == x]
rg_major = recent_grads[recent_grads["Major"] == x]
if rg_major["Unemployment_rate"] < aa_major["Unemployment_rate"]:
rg_lower_count += 1
print(rg_lower_count)
You can't do this anymore. When you compare <
, >
you need to have the indices lined up . reindex
one of your series like the other.
I'd edit your code like this
import pandas as pd
majors = recent_grads['Major'].unique()
rg_lower_count = 0
for x in majors:
aa_major = all_ages[all_ages['Major'] == x]
rg_major = recent_grads[recent_grads["Major"] == x]
rg_major_unemp = rg_major["Unemployment_rate"]
aa_major_unemp = aa_major["Unemployment_rate"].reindex_like(rg_major_unemp)
if rg_major_unemp < aa_major_unemp:
rg_lower_count += 1
print(rg_lower_count)
Same example as in linked question
import pandas as pd
x = pd.Series([1, 1, 1, 0, 0, 0], index=['a', 'b', 'c', 'd', 'e', 'f'], name='Value')
y = pd.Series([0, 2, 0, 2, 0, 2], index=['c', 'f', 'a', 'e', 'b', 'd'], name='Value')
x > y
ValueError: Can only compare identically-labeled Series objects
x.reindex_like(y) > y
c True
f False
a True
e False
b True
d False
Name: Value, dtype: bool
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.