简体   繁体   English

如何根据另一列填充列的第一行中的NA值

[英]How to fill NA values in first row of the column on basis of another column

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) 我有一个csv文件,其中一个列我需要根据上面同一列中提供的数据填充NA值。例如,假设我们说列数据就像这个chrome,NA,NA,Explorer,NA我希望它是chrome,chrome,chrome,Explorer,Explorer ,我能够实现这一点,但现在我有另一个问题,让我们说列数据就像这个NA,NA,chrome,Explorer,NA我输出为NA,NA,chrome,Explorer,Explorer (这里的NA在第一行输出部分我想用同一行中的另一列数据替换)

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- 这是输入csv数据 -

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 在上面的例子中,第一行中的Apllication名称丢失,甚至我们也不能使用fillna,因为它是csv的第一行,所以我想使用title
data to be used in Apllication name column which basically means i want output like this- 在Apllication name列中使用的数据,这基本上意味着我希望输出像这样 -

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

您可以使用行来使用向后和向前填充:

df = df.ffill(axis ='rows').bfill(axis ='rows')   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM