简体   繁体   中英

How to split a nested list by first three commas? Python

my_df = pd.DataFrame({'ID':['12345','23456','34567'],
     'Info':(['Rob Kardashian', '00052369', '1987-03-17', 'Reality Star'],
                  ['Brooke Barry', '00213658', '2001-03-30', '100','Best','TikTok Star'],
                 ['Stan Lee','35239856','1922-12-28','10','Best Publisher & Producer'])})

在此处输入图片说明

I have above dataframe and I want to split the values in column 'Info' by the first three commas.

The code below only works for splitting by all commas...

[[re.split(',', i) for i in w] for w in my_df['Info']]

The expected result: 在此处输入图片说明

How about something simple like this?

[[*x[:3], ', '.join(x[3:])] for x in my_df['Info']]

It would return this output:

[['Rob Kardashian', '00052369', '1987-03-17', 'Reality Star'], ['Brooke Barry', '00213658', '2001-03-30', '100, Best, TikTok Star'], ['Stan Lee', '35239856', '1922-12-28', '10, Best Publisher & Producer']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM