I'm trying to add a feature column to my dataframe and match it to my existing dataframe rows by month and year (which I've stored in integer columns).
I've tried using .iloc[]
to specify the row to add the new feature variable df['Price Level']
that is taken from i_df['CPIAUCNS']
, but after reading a lot of Stack Overflow, it seems like np.where
is a more appropriate function for a conditional statement.
bool_filter = ((df['Release Date Year'] == i_df['Release Date Year'])
& (df['Release Date Month'] == i_df['Release Date Month']))
df['Price Level'] = np.where(bool_filter, i_df['CPIAUCNS'])
I was hoping this would generate a new feature column in df
with the value from i_df
where Year and Month were equal, instead I receive:
ValueError: Can only compare identically-labeled Series objects
This error is thrown in the bool_filter
so the np.where
does not get to execute.
Would someone be able to explain why this conditional statement generates this error and how I might be able to rephrase it?
EDIT:
Trying to use .values()
in the boolean filter results in the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-34-9b470b5aee2c> in <module>()
5 # df[df['Release Date'].isna() == True]
6
----> 7 bool_filter = ((df['Release Date Year'].values() == i_df['Release Date Year'].values())
8 & (df['Release Date Month'].values() == i_df['Release Date Month'].values()))
9
TypeError: 'numpy.ndarray' object is not callable