Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Pandas - ValueError: Incompatible indexer with Series

Tags:

python

pandas

I have a dataframe:

df:

        A      B 
id
 3   'Yes'    23
 5   'Yes'    67
 6    'No'    56
 8    'No'    23

I have another dataframe:

calc:
       A    B
id   
 3   'No'   4 

I would like to update df with calc values. I'm trying to use the following:

tgsm.loc[i]=calc

However, that doesn't work. I keep getting the following error:

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/indexing.py", line 693, in _align_series
raise ValueError('Incompatible indexer with Series')
ValueError: Incompatible indexer with Series

If try tgsm.loc[i]=calc[i], it gets me to this other error:

File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 3

Can anyone help enlightening my journey?

like image 283
aabujamra Avatar asked Jan 31 '23 03:01

aabujamra


1 Answers

You can use the update method to directly overwrite it in place.

df.update(calc)
like image 100
Ted Petrou Avatar answered Feb 02 '23 09:02

Ted Petrou