splitting at underscore in python and storing the first value

I have a pandas data frame like df with a column construct_name

construct_name
aaaa_t1_2    
cccc_t4_10
bbbb_g3_3

and so on. I want to first split all the names at the underscore and store the first element (aaaa,cccc, etc.) as another column name.

Expected output

construct_name  name
aaaa_t1_2       aaaa
cccc_t4_10      bbbb

and so on.

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]

and so on

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

Answers


Just use the vectorised str method split and use integer indexing on 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

After you do the split, you should get the first element (using [0]). And not after the 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

split take an optional argument maxsplit:

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

Need Your Help

Android: Binding data from a database to a CheckBox in a ListView?

android listview checkbox cursor

I'm trying to bind data from my SQLiteDatabase to a ListView. I'm currently using a SimpleCursorAdapter to fill in my ListView. Unfortunately this doesn't seem to work with setting a CheckBox's che...

Why is my custom MySQL function so much slower than inlining same in query?

mysql performance user-defined-functions

I repeatedly use this SELECT query to read unsigned integers representing IPv4 addresses and present them as human readable dotted quad strings.