最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Dataframe fill for null values - Pandas - Stack Overflow

programmeradmin1浏览0评论

I encounter problems trying to fill all null values on a specific column of a data frame.

Here is an example of dataframe and my expected outcome.

Example data frame:

Column_1 Column_2
    F      A
   None    B
   None   None
    G      C
   None   None
   None    D 
    H      D

I want to get the first value from the column 1 to all null value from column 2

Expected Outcome:

Column_1 Column_2
    F      A
   None    B
   None    G #First value from the left column
    G      C
   None    H #First value from the left column
   None    D 
    H      D

I'm getting error when I try this code.

df['Colunmn_2'].ffill(df.loc[df['Column_1'].first_valid_index(), 'Column_1'],inplace=True)

Thanks in advance!

I encounter problems trying to fill all null values on a specific column of a data frame.

Here is an example of dataframe and my expected outcome.

Example data frame:

Column_1 Column_2
    F      A
   None    B
   None   None
    G      C
   None   None
   None    D 
    H      D

I want to get the first value from the column 1 to all null value from column 2

Expected Outcome:

Column_1 Column_2
    F      A
   None    B
   None    G #First value from the left column
    G      C
   None    H #First value from the left column
   None    D 
    H      D

I'm getting error when I try this code.

df['Colunmn_2'].ffill(df.loc[df['Column_1'].first_valid_index(), 'Column_1'],inplace=True)

Thanks in advance!

Share Improve this question asked Mar 13 at 16:55 Rodel SilvanoRodel Silvano 437 bronze badges 3
  • What should happen if you already have a non-NaN value in column_1 and a NaN in column_2? – mozway Commented Mar 13 at 17:15
  • The non-NaN value from column_1 should replace the NaN in column_2 – Rodel Silvano Commented Mar 13 at 17:25
  • OK, then my answer below is doing exactly that. – mozway Commented Mar 13 at 17:35
Add a comment  | 

1 Answer 1

Reset to default 4

You can combine fillna on Column_2 and bfill on Column_1:

df['Column_2'] = df['Column_2'].fillna(df['Column_1'].bfill())

Output:

  Column_1 Column_2
0        F        A
1      NaN        B
2      NaN        G
3        G        C
4      NaN        H
5      NaN        D
6        H        D

Intermediates:

  Column_1 Column_2 col1_bfill col2_fillna
0        F        A          F           A
1      NaN        B          G           B
2      NaN      NaN          G  ------>  G
3        G        C          G           C
4      NaN      NaN          H  ------>  H
5      NaN        D          H           D
6        H        D          H           D
发布评论

评论列表(0)

  1. 暂无评论