简体   繁体   中英

Fill Consecutive NANs in a column of a dataframe

I have a dataframe having a column C, I want to fill consecutive blanks by the same number because later I need to group that row.

eg

A B C
 1 2 Nan
 1 2 Nan
 1 2 3
 1 2 Nan
 1 2 Nan

the output I want is something like this

A B C
1 2 1
1 2 1
1 2 3
1 2 2
1 2 2

I tried using shift() to compare but didn't come to the desired output.

You can use fillna by new Series created by cumsum by boolean mask :

df['C'] = df['C'].fillna(df['C'].notnull().cumsum() + 1)

print (df)
   A  B    C
0  1  2  1.0
1  1  2  1.0
2  1  2  3.0
3  1  2  2.0
4  1  2  2.0

Detail :

print (df['C'].notnull().cumsum())
0    0
1    0
2    1
3    1
4    1
Name: C, dtype: int32

The function fillna is your solution:

dataframe['yourColumn'] = dataframe['yourColumn'] .fillna( 1 , inplace=True)

Moreover you can put whatever value you want to substitute the nan values. For instance, you coul set the mean:

dataframe['yourColumn']= dataframe['yourColumn'].fillna(dataset['yourColumn'] .mean(), inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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