sheel
sheel

Reputation: 509

How to fill multiple list by 0's in Pandas data frame?

I have Pandas data frame and I am trying to add 0's in those lists where numbers are missing. In the below data frame, the max length of the list is 4 which is in the 3rd position. accordingly, I will add 0's to the remaining lists.

Input:

        Lists
0     [158, 202]
1     [609, 405]
2     [544, 20]
3     [90, 346, 130, 202]
4     [6]

Output:

        Lists
0     [158, 202, 0, 0]
1     [609, 405, 0, 0]
2     [544, 20, 0, 0]
3     [90, 346, 130, 202]
4     [6, 0, 0, 0]

Upvotes: 2

Views: 91

Answers (2)

Akshay Sehgal
Akshay Sehgal

Reputation: 19332

Another way to do this would be using apply with a lambda function -

maxlen = df['Lists'].str.len().max() #as suggested by Anky, better than an apply since vectorised
f = lambda x: x + ([0] * (maxlen - len(x)))

df['Padded'] = df['Lists'].apply(f)
print(df)
                 Lists               Padded
0           [158, 202]     [158, 202, 0, 0]
1           [609, 405]     [609, 405, 0, 0]
2            [544, 20]      [544, 20, 0, 0]
3  [90, 346, 130, 202]  [90, 346, 130, 202]
4                  [6]         [6, 0, 0, 0]

Upvotes: 2

anky
anky

Reputation: 75100

You can convert to dataframe and fillna with 0 then agg to list.

df.assign(New=pd.DataFrame(df['Lists'].tolist()).fillna(0).astype(int).agg(list,1))

                 Lists                  New
0           [158, 202]     [158, 202, 0, 0]
1           [609, 405]     [609, 405, 0, 0]
2            [544, 20]      [544, 20, 0, 0]
3  [90, 346, 130, 202]  [90, 346, 130, 202]
4                  [6]         [6, 0, 0, 0]


#df['Lists'] = pd.DataFrame(df['Lists'].tolist()).fillna(0).astype(int).agg(list,1)

Or as suggested by @ShubhamSharma, you can call to_numpy().tolist() too:

df.assign(New=pd.DataFrame([*df['Lists']]).fillna(0).astype(int).to_numpy().tolist())

Upvotes: 2

Related Questions