Home > Blockchain >  Python Map function deletes all data in column
Python Map function deletes all data in column

Time:08-13

I have a Pandas DataFrame with several columns. One of these ('Code') is object-type but has missing data (NaN). Other data can be numbers or letters. For the missing data, I want to do a map / set_index function in order to fill in the data. Here is my code:

for row in df['Code']:
    if pd.isnull(row) == True:
        df['Code']= df['account'].map(df_2.set_index('AccountID')['AccountCode'])
    else:
        None

However, this code deletes all data from the entire columns. This is the original (I mean to do the map function on the NaN values only!) :

0      23050178040
1      23050178040
2      23050178040
3      23050178106
4      23050178040
          ...     
288    23050942326
289    23050942326
290            NaN
291    23050942858
292            NaN
Name: Code BU, Length: 293, dtype: object

And the result:

0      NaN
1      NaN
2      NaN
3      NaN
4      NaN
      ... 
288    NaN
289    NaN
290    NaN
291    NaN
292    NaN
Name: Code BU, Length: 293, dtype: object

What is the issue here?

CodePudding user response:

Instead all your code loop use Series.fillna:

df['Code']= df['Code'].fillna(df['account'].map(df_2.set_index('AccountID')['AccountCode']))
  • Related