简体   繁体   中英

How do I create new columns based off of a list derived from an existing column in python/pandas?

I have a data frame with a column entitle "Name" that includes a string in this format: "Group1name / Group2name / Group3name / Group4name"

I want to create 3 new columns based off of the "Name" column and the "/" delimiter:

Level 1: "Group1name"
Level 2: "Group1name / Group2name"
Level 3: "Group1name / Group2name / Group3name"

How do I create these new columns in the dataframe?

This solution uses a generator expression, which is basically a nested for loop. It splits the string found in the Name column of df based on the / delimiter. It then joins it back together, but only takes the first n elements for the appropriate column when joining back together.

df = pd.DataFrame({'Name': ["Group1name / Group2name / Group3name / Group4name"]})

for n in range(1, 4):  # 1, 2, 3 for column indexing and naming.
    df['col_{0}'.format(n)] = ' / '.join(group for groups in df.Name.str.split(' / ') 
                                         for group in groups[:n])

>>> df.T
                                                       0
Name   Group1name / Group2name / Group3name / Group4name
col_1                                         Group1name
col_2                            Group1name / Group2name
col_3               Group1name / Group2name / Group3name

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