pandas - splitting at underscore in python and storing the first value -
i have pandas data frame df column construct_name
construct_name aaaa_t1_2 cccc_t4_10 bbbb_g3_3
and on. want first split names @ underscore , store first element (aaaa,cccc, etc.) column name.
expected output
construct_name name aaaa_t1_2 aaaa cccc_t4_10 bbbb
and on.
i tried following df['construct_name'].map(lambda row:row.split("_"))
, gives me list like
[aaaa,t1,2] [cccc,t4,10]
and on
but when
df['construct_name'].map(lambda row:row.split("_"))[0]
first element of list error. can suggest fix. thanks
just use vectorised str
method split
, use integer indexing on list 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
Comments
Post a Comment