gpt4 book ai didi

python - Pandas :使用正则表达式更新条件列

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:36 28 4
gpt4 key购买 nike

所以我有一个列,它是 Pandas Dataframe 中列的对象类型。

它包括以下数据:

array(['9.4', '9.8', '10', '9.5', '10.5', '9.2', '9.9', '9.1', '9.3', '9',
'9.7', '10.1', '10.6', '9.6', '10.8', '10.3', '13.1', '10.2',
'10.9', '10.7', '12.9', '10.4', '13', '14', '11.5', '11.4', '12.4',
'11', '12.2', '12.8', '12.6', '12.5', '11.7', '11.3', '12.3', '12',
'11.9', '11.8', '8.7', '13.3', '11.2', '11.6', '11.1', '13.4',
'12.1', '8.4', '12.7', '14.9', '13.2', '13.6', '13.5',
'100.333.333.333.333', '9.55', '8.5', '110.666.666.666.667',
'956.666.666.666.667', '10.55', '8.8', '135.666.666.666.667',
'11.95', '9.95', '923.333.333.333.333', '9.25', '9.05', '10.75',
'8.6', '8.9', '13.9', '13.7', '8', '8.0', '14.2', '11.94',
'128.933.333.333.333', '114.666.666.666.667', '10.98',
'114.333.333.333.333', '105.333.333.333.333',
'953.333.333.333.333', '109.333.333.333.333',
'113.666.666.666.667', '113.333.333.333.333',
'973.333.333.333.333', '11.05', '9.75', '11.35', '11.45', '14.05',
'123.333.333.333.333', '12.75', '13.8', '12.15', '13.05',
'112.666.666.666.667', '105.666.666.666.667',
'117.333.333.333.333', '11.75', '10.65', '109.666.666.666.667',
'101.333.333.333.333', '10.15', '104.666.666.666.667',
'116.333.333.333.333', '12.25', '11.85', '11.65', '13.55',
'131.333.333.333.333', '120.666.666.666.667', '11.55',
'963.333.333.333.333', '12.05'], dtype=object)

我想用 0 或其他东西更新具有多个点 ('.') 的那些。我不是很熟悉 regex-es,但我的想法是使用 regex 来解决这个问题,而不是 '953.333.333.333.333'!

DF.replace({'column': '953.333.333.333.333'},'0')

非常感谢!

最佳答案

使用 numpy.whereSeries.str.countSeries.gt :

DF['column'] = np.where(DF['column'].str.count('\.').gt(1), 0, DF['column'])

[输出]

array(['9.4', '9.8', '10', '9.5', '10.5', '9.2', '9.9', '9.1', '9.3', '9',
'9.7', '10.1', '10.6', '9.6', '10.8', '10.3', '13.1', '10.2',
'10.9', '10.7', '12.9', '10.4', '13', '14', '11.5', '11.4', '12.4',
'11', '12.2', '12.8', '12.6', '12.5', '11.7', '11.3', '12.3', '12',
'11.9', '11.8', '8.7', '13.3', '11.2', '11.6', '11.1', '13.4',
'12.1', '8.4', '12.7', '14.9', '13.2', '13.6', '13.5', 0, '9.55',
'8.5', 0, 0, '10.55', '8.8', 0, '11.95', '9.95', 0, '9.25', '9.05',
'10.75', '8.6', '8.9', '13.9', '13.7', '8', '8.0', '14.2', '11.94',
0, 0, '10.98', 0, 0, 0, 0, 0, 0, 0, '11.05', '9.75', '11.35',
'11.45', '14.05', 0, '12.75', '13.8', '12.15', '13.05', 0, 0, 0,
'11.75', '10.65', 0, 0, '10.15', 0, 0, '12.25', '11.85', '11.65',
'13.55', 0, 0, '11.55', 0, '12.05'], dtype=object)

关于python - Pandas :使用正则表达式更新条件列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429224/

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