Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Data Science by (17.6k points)

I have a Pandas DataFrame which contains a column name RecentDelays in which it contains a list of element. My DataFrame

Need to break this RecentDelays columns into N different column such as Delay1,Delay2,....with first value of list in Delay1 column of corresponding row,second value in Delay2 column of corresponding row and so on .If there in no nth value it should be NaN

1 Answer

0 votes
by (41.4k points)

Here, since .apply(pd.Series) is slow, so ,use DataFrame constructor:

# data sample

d1 = pd.DataFrame({'Airline':['A','B','C'],'Delays':[[],[1],[1,2]]})

d2 = (pd.DataFrame(d1['Delays'].values.tolist(), index=d1.index)

        .rename(columns = lambda x: 'Delay{}'.format(x+1)))

df = d1.join(d2)

print (df)

  Airline  Delays  Delay1  Delay2

0       A      []     NaN     NaN

1       B     [1]     1.0     NaN

2       C  [1, 2]     1.0     2.0

Use pop first if there is a need to remove column :

d2 = (pd.DataFrame(d1.pop('Delays').values.tolist(), index=d1.index)

        .rename(columns = lambda x: 'Delay{}'.format(x+1)))

df = d1.join(d2)

print (df)

  Airline  Delay1  Delay2

0       A     NaN     NaN

1       B     1.0     NaN

2       C     1.0     2.0

If you wish to learn Pandas visit this Pandas Tutorial.

Browse Categories

...