Home > front end >  Remove elements stored as a list in a dataframe column from list structures and convert to a string
Remove elements stored as a list in a dataframe column from list structures and convert to a string

Time:01-13

Is it possible to remove the comma separated date elements under the column df['date'] from the list structure and store as a string instead? example dataframe:

df=pd.DataFrame({'date':[['2022-06-24'],['2021-07-07','2021-07-14'],\
                         ['2021-08-11','2021-12-17','2021-09-14','2022-02-15'],\
                             ['2019-08-19','2019-09-25'],\
                                 ['2013-05-16']]})

Output should look like this:

2022-06-24
2021-07-07,2021-07-14
2021-08-11,2021-12-17,2021-09-14,2022-02-15
2019-08-19,2019-09-25
2013-05-16

I tried:

df['date_2'] = [','.join(map(str, l)) for l in df['date']]

but not getting the desired output

CodePudding user response:

Explode your lists then group by index and join all dates:

>>> df['date'].explode().groupby(level=0).agg(','.join)
0                                     2022-06-24
1                          2021-07-07,2021-07-14
2    2021-08-11,2021-12-17,2021-09-14,2022-02-15
3                          2019-08-19,2019-09-25
4                                     2013-05-16
Name: date, dtype: object

Alternative:

>>> df['date'].apply(lambda x: ','.join(x))
0                                     2022-06-24
1                          2021-07-07,2021-07-14
2    2021-08-11,2021-12-17,2021-09-14,2022-02-15
3                          2019-08-19,2019-09-25
4                                     2013-05-16
Name: date, dtype: object

Suggested by @jezrael (the best solution)

>>> df['date'].str.join(',')
0                                     2022-06-24
1                          2021-07-07,2021-07-14
2    2021-08-11,2021-12-17,2021-09-14,2022-02-15
3                          2019-08-19,2019-09-25
4                                     2013-05-16
Name: date, dtype: object
  • Related