gpt4 book ai didi

Python用前缀分割字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:09 24 4
gpt4 key购买 nike

如果我有一个充满文本和价格的数据框列。

 0  £75 BT Reward Card
1 £125 BT Reward Card
2 £50 Retail Voucher
3 £100 BT Reward Card
4 £150 BT Reward Card
5 £50 Cashback
6 Fibre Connection Fee (£50 Credit
7 £75 BT Reward Card
8 £125 BT Reward Card
9 £50 Cashback
10 £0 Fibre Connection Fee (£50 Credit

我只想在£符号后面直接返回数字。

到目前为止我已经知道了,但是对于索引 6 和 10 来说,它会分崩离析

df['col']=df['col'].apply(lambda x: x.split(' ')  [0])

我也试过这个:

df['col']=df['col'].apply(lambda x: x.split('£')  [1])

最佳答案

如果需要第一个值只使用extract并在必要时转换为整数:

df['new'] = df['col'].str.extract('£(\d+)').astype(int)
print (df)
col new
0 £75 BT Reward Card 75
1 £125 BT Reward Card 125
2 £50 Retail Voucher 50
3 £100 BT Reward Card 100
4 £150 BT Reward Card 150
5 £50 Cashback 50
6 Fibre Connection Fee (£50 Credit 50
7 £75 BT Reward Card 75
8 £125 BT Reward Card 125
9 £50 Cashback 50
10 £0 Fibre Connection Fee (£50 Credit 0

如果列表中的所有值都使用 str.findall :

#values are strings
df['new'] = df['col'].str.findall('£(\d+)')
#values are integers
#df['new'] = df['col'].str.findall('£(\d+)').apply(lambda x: [int(y) for y in x])
print (df)
col new
0 £75 BT Reward Card [75]
1 £125 BT Reward Card [125]
2 £50 Retail Voucher [50]
3 £100 BT Reward Card [100]
4 £150 BT Reward Card [150]
5 £50 Cashback [50]
6 Fibre Connection Fee (£50 Credit [50]
7 £75 BT Reward Card [75]
8 £125 BT Reward Card [125]
9 £50 Cashback [50]
10 £0 Fibre Connection Fee (£50 Credit [0, 50]

如果在新列中需要它们,请使用 extractallunstack , add_prefixjoin :

df = df.join(df['col'].str.extractall('£(\d+)')[0].unstack().astype(float).add_prefix('new'))
print (df)
col new0 new1
0 £75 BT Reward Card 75.0 NaN
1 £125 BT Reward Card 125.0 NaN
2 £50 Retail Voucher 50.0 NaN
3 £100 BT Reward Card 100.0 NaN
4 £150 BT Reward Card 150.0 NaN
5 £50 Cashback 50.0 NaN
6 Fibre Connection Fee (£50 Credit 50.0 NaN
7 £75 BT Reward Card 75.0 NaN
8 £125 BT Reward Card 125.0 NaN
9 £50 Cashback 50.0 NaN
10 £0 Fibre Connection Fee (£50 Credit 0.0 50.0

关于Python用前缀分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54868242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com