Reputation: 157
I have a csv file where in one of the column i need to fill NA values on the basis of data which is available above in same column.For example lets say if lets say column data is like this chrome,NA,NA,Explorer,NA
I want it to be chrome,chrome,chrome,Explorer,Explorer
and i am able to achieve this but now i have another problem lets say the column data is like this NA,NA,chrome,Explorer,NA
I get output as NA,NA,chrome,Explorer,Explorer
(here NA in first row Output Section I want to replace by another column data in same row)
In order to fill missing values on the basis of data from same column i used this code-
filling a missing value with previous ones
data["Application Name"].fillna(method ='pad', inplace = True)
data.iloc[:,4].fillna(method ='pad', inplace = True)
data.head()
Here is the Input csv Data-
Sequence Number Action Name Title Recorder Type Application Name
1 1 ANT Logger
2 5 Chrome Recorder
3 5 Chrome Recorder Chrome
4 5 Chrome Recorder Chrome
5 5 Chrome Recorder Chrome
6 6 xyz Chrome Recorder Chrome
7 7 Chrome Recorder Chrome
In the above example Apllication name in First row is missing and there even we can't use fillna because it is the first row of csv so i want to use title
data to be used in Apllication name column which basically means i want output like this-
Action Name Title Recorder Type Application Name
1 ANT Logger ANT Logger
5 Chrome Recorder ANT Logger
5 Chrome Recorder Chrome
5 Chrome Recorder Chrome
5 Chrome Recorder Chrome
6 xyz Chrome Recorder Chrome
7 Chrome Recorder Chrome
Upvotes: 1
Views: 1060
Reputation: 2331
you can use backward and forward fill using rows:
df = df.ffill(axis ='rows').bfill(axis ='rows')
Upvotes: 3