Quantcast
Channel: How to replace NaN column value with the previous column value in pandas dataframe? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to replace NaN column value with the previous column value in pandas dataframe?

$
0
0

I am trying to merge two dataframes that does not have equal number of rows or columns. It results in NaN values. I want to fill this NaN values with the previous value in the column.

import pandas as pdimport numpy as npdflist = [[1, "a", "b"], [2, "a", "b"], [3, "a", "b"]]df = pd.DataFrame(dflist)dflist1 = [[1, "a", "b", "c", "e"], [1, "a", "b", "c", "e"], [2, "a", "b", "c", "e"], [3, "a", "b", "c", "e"], [1, "a", "b", "c", "e"],[4, "a", "b", "c", "e"], [5, "a", "b", "c", "e"]]df1 = pd.DataFrame(dflist1)df.columns = ["col1", "col2", "col3"]df1.columns = ["col1", "col21", "col31", "col45", "col56"]result = pd.merge(df1, df, how='outer')print(result)

It results in

   col1 col21 col31 col45 col56 col2 col30     1     a     b     c     e    a    b1     1     a     b     c     e    a    b2     1     a     b     c     e    a    b3     2     a     b     c     e    a    b4     3     a     b     c     e    a    b5     4     a     b     c     e  NaN  NaN6     5     a     b     c     e  NaN  NaN

But the desired table should be filled with previous values of the NaN,

col1 col21 col31 col45 col56 col2 col30     1     a     b     c     e    a    b1     1     a     b     c     e    a    b2     1     a     b     c     e    a    b3     2     a     b     c     e    a    b4     3     a     b     c     e    a    b5     4     a     b     c     e    a    b6     5     a     b     c     e    a    b

What I tired to do is to get the indices of NaN values but it is not giving the desired result.

indices = list(np.where(result['col3'].isna()[0]))print(indices)

Results in [array([], dtype=int64)]

How can this be accomplished?


Viewing all articles
Browse latest Browse all 2

Trending Articles