python获取文件名中两条下划线之间的部分_在python中的下划线处分割并存储第一个值...

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

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值