Splitting on underscore in python and storing the first value

I have a pandas data frame like df named column_name

construct_name
aaaa_t1_2    
cccc_t4_10
bbbb_g3_3

etc. I want to first break all the names underlined and save the first element (aaaa, cccc, etc.) as another column name.

Expected Result

construct_name  name
aaaa_t1_2       aaaa
cccc_t4_10      bbbb

etc.

I tried the following df['construct_name'].map(lambda row:row.split("_"))and it gives me a list like

[aaaa,t1,2]
[cccc,t4,10]

etc.

But when I do

df['construct_name'].map(lambda row:row.split("_"))[0], to get the first element of the list, I get an error. Can you suggest a fix. Thanks

+4
source share
3 answers

Just use the vector strmethod splitand use integer indexing in the list to get the first element:

In [228]:

df['first'] = df['construct_name'].str.split('_').str[0]
df
Out[228]:
  construct_name first
0      aaaa_t1_2  aaaa
1     cccc_t4_10  cccc
2      bbbb_g3_3  bbbb
+12

split ( [0]). map.:

In [608]: temp['name'] = temp['construct_name'].map(lambda v: v.split('_')[0])

In [609]: temp
Out[609]: 
  construct_name  name
0      aaaa_t1_2  aaaa
1     cccc_t4_10  cccc
2      bbbb_g3_3  bbbb
+2

split maxsplit:

>>> construct_name = 'aaaa_t1_2'
>>> name, rest = construct_name.split('_', 1)
>>> name
'aaaa'
0
source

All Articles