3

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)
  • `rg_major` and `aa_major` must have different indices, and that may be where the issue is coming from. You may have to apply `.reset_index` on both `aa_major` and `rg_major` before comparing. Also, you may have to fix the indentation here. The `if-statement` may need to be inside the `for-loop`. – Abdou Feb 01 '17 at 00:53
  • @Abdou Thank you fixed the indentation. When i do `.reset_index` it doesnt wok for me but I will play with it more to see if I can get it to work. Thank you for your feedback. – javiercaudillo10 Feb 01 '17 at 01:40
  • I played more with this code and the if statement works in two ways: `if float(series[column]) < float(series[column]):` and `if series[column].item() < series[column].item()`. – javiercaudillo10 Feb 01 '17 at 18:04

1 Answers1

2

See this old question of mine

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)

Demonstration

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
Community
  • 1
  • 1
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • This worked great for me. I didn't know about the `.reindex_like` attribute but I see it being useful. Before i did this however i managed to get my code to work by doing `if float(series[column]) < float(series[column]):` i dont know if that is a correct way of fixing this problem? – javiercaudillo10 Feb 01 '17 at 17:54