简体   繁体   中英

pandas dataframe - two column string match and group

I have a pandas dataframe which contains strings in two columns. I want to for each of the columns extract all strings which are similar except the numerical digits and add new columns where the similar text is exchanged against a idx value.

From this:

Id    Name1    Name2
0     Alpha 1  Bravo 3
1     Alpha 2  Alpha 2
2     Bravo 3  Alpha 1

To This:

Id    Name1    Name2    NewCol1    NewCol2
0     Alpha 1  Bravo 3  1          2
1     Alpha 2  Zero  2  1          3
2     Bravo 3  Alpha 1  2          1

Is there a simple solution to this without a big iteration loop?

I think need create Series with MultiIndex by stack , remove digit s and for categories use factorize , last unstack and join to original:

s = df.set_index('Id').stack().str.replace('\d+', '')

df = df.join(pd.Series(pd.factorize(s)[0] + 1, index=s.index).unstack().add_prefix('New'))
print (df)
   Id    Name1    Name2  NewName1  NewName2
0   0  Alpha 1  Bravo 3         1         2
1   1  Alpha 2   Zero 2         1         3
2   2  Bravo 3  Alpha 1         2         1

Details :

print (s)
Id       
0   Name1    Alpha 
    Name2    Bravo 
1   Name1    Alpha 
    Name2     Zero 
2   Name1    Bravo 
    Name2    Alpha 
dtype: object

print (pd.factorize(s)[0] + 1)
[1 2 1 3 2 1]

You may need to use a loop to iterate over column names. For rows use pandas.Series.str.replace

import pandas as pd
df = pd.DataFrame({'Name1' :['Alpha 1', 'Aplha 2', 'Bravo 3'], 'Name2' : ['Bravo 3', 'Alpha 2', 'Alpha 1']})
for name in df.columns.tolist():
    df["newCol" + name.replace("Name", "")] = df[name].str.split(expand=True)[1]

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