I am trying to update/modify certain part of data frame based on another column's value.
If column ['a']
is null, fill column ['a']
with value of column ['b']
like below
list_position = [[4, 35]]
df.iloc[list_position[0][0]:list_position[0][1] + 1,:]['a'] = df.iloc[list_position[0][0]:list_position[0][1] + 1,:].apply(lambda row: row['a'] * row['b'] if np.isnan(row['a']) else row['b'], axis=1)
It is giving error as TypeError: an integer is required
.
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
Any suggestion to correct the same is highly appreciated.
Update 1. I tried all three ways as suggested in 1 duplicate answer
df['Cat1'].fillna(df['Cat2'])
and
2 answers suggested on this post.
1. df['a'][df['a'].isnull()] = df['b']
2. df['a'] = df['a'].fillna(df['b'])
All are giving same error as:
Traceback (most recent call last):
File "pandas\_libs\index.pyx", line 162, in
pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 958, in
pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
It is working if I replace column name with column number like
df[7] = df[7].fillna(df[8)
Not sure why, if any one has explanation for same.