Replace NaN values with specific text based on adjacent column in pandas dataframe

Viewed 332

My data has some NaN values in its first column. To replace these NaN values, I want to look to the adjacent value in the next column and put a specific value as replaceNaN. Here is the csv data:

,Release,SubRelease,ReleaseDate,TypeofRelease,Package
0,LTE,TL101,2017-09-27,Major Update,2.0.4
1,LTE,TL101,2017-09-26,Normal Update,3.1
2,NaN,TL209,2017-09-25,Major Update,3.2
3,5G,5GS,2017-09-25,Delivery,1.1
4,NaN,5GM,2017-09-24,Release,1.0
5,LTE,FL18A,2017-09-23,Normal Update,3.0

For eg, there is a NaN value in the 3rd row of Release. I want to look at the SubRelease column of the same row, and say that since the value here is "TL209", I want to replace the NaN with the value "LTE". Similarly, if the value in SubRelease column is "5G19", I want to replace the NaN with "5G" for Release.

The first thing that comes to my mind is using regex, specify to look if the value in SubRelease column contains or begins with the text "5G". But I don't know how to implement this. Or is there any better approach?

Easier to view csv data:

csv data

2 Answers

You can just do:

df["Release"] = df["Release"].fillna(df["SubRelease"])
df
>>>   Release SubRelease ReleaseDate  TypeofRelease Package
0     LTE      TL101  2017-09-27   Major Update   2.0.4
1     LTE      TL101  2017-09-26  Normal Update     3.1
2   TL209      TL209  2017-09-25   Major Update     3.2
3      5G        5GS  2017-09-25       Delivery     1.1
4     5GM        5GM  2017-09-24        Release     1.0
5     LTE      FL18A  2017-09-23  Normal Update     3.0

Edit I misread the question so forgot this last step:

df = df.replace({"Release":{"TL209":"LTE", "5GM":"5G", "5GS":"5G", ...}})
>>>   Release SubRelease ReleaseDate  TypeofRelease Package
0     LTE      TL101  2017-09-27   Major Update   2.0.4
1     LTE      TL101  2017-09-26  Normal Update     3.1
2     LTE      TL209  2017-09-25   Major Update     3.2
3      5G        5GS  2017-09-25       Delivery     1.1
4      5G        5GM  2017-09-24        Release     1.0
5     LTE      FL18A  2017-09-23  Normal Update     3.0

To replace all NaNs in the Release column depending on the values in SubRelease, you can find e.g. all '5G' sub-releases and replace these NaNs first. If there are more conditions, these can be replaced in the same way. In the end, replace any remaining NaNs with the default value (here 'LTE').

This can be done using loc together with an appropriate mask:

df.loc[df['Release'].isna() & df['SubRelease'].str.contains('5G'), 'Release'] = '5G'
df = df.fillna('LTE')

Result:

  Release SubRelease ReleaseDate  TypeofRelease Package
0     LTE      TL101  2017-09-27   Major Update   2.0.4
1     LTE      TL101  2017-09-26  Normal Update     3.1
2     LTE      TL209  2017-09-25   Major Update     3.2
3      5G        5GS  2017-09-25       Delivery     1.1
4      5G        5GM  2017-09-24        Release     1.0
5     LTE      FL18A  2017-09-23  Normal Update     3.0
Related