我有一个列的数据帧
id bins
1 (2, 3]
2 (4, 5]
3 (6, 7]
4 (8, 9]
5 (10, 11] 我想得到这样的东西。
id bins
1 2 - 3
2 4 - 5
3 6 - 7
4 8 - 9
5 10 - 11 我的目标是使用regex来实现这一点。恐怕我不是那个regex专家。这在一定程度上是我尝试过但没有成功的解决办法。
df['bins'].astype(str).str.replace(']', ' ')
df['bins'].astype(str).str.replace(',', ' - ')
df['bins'] = df['bins'].apply(lambda x: x.replace('[','').replace(']',''))任何帮助都将不胜感激!
提前感谢
发布于 2021-08-30 13:45:29
您可以使用
df['bins'] = df['bins'].astype(str).str.replace(r'[][()]+', '', regex=True).str.replace(',', ' - ')注意:
.replace(r'[][()]+', '', regex=True) -删除一个或多个]、[、(和) chars.str.replace(',', ' - ') --用space+-+space.替换所有逗号。
另一种方式是:
df['bins'].astype(str).str.replace(r'\((\d+)\s*,\s*(\d+)]', r'\1 - \2', regex=True)在这里,\((\d+)\s*,\s*(\d+)]匹配
\( -a ( char(\d+) - Group 1 (\1):一个或多个digits\s*,\s* -一个以零或多个whitespaces(\d+)括起来的逗号-第2组(\2):一个或多个digits] -一个] char.熊猫测试:
>>> import pandas as pd
>>> df = pd.DataFrame({'bins':['(2, 3]']})
>>> df['bins'].astype(str).str.replace(r'\((\d+)\s*,\s*(\d+)]', r'\1 - \2', regex=True)
0 2 - 3
Name: bins, dtype: object
>>> df['bins'].astype(str).str.replace(r'[][()]+', '', regex=True).str.replace(',', ' - ')
0 2 - 3
Name: bins, dtype: object发布于 2021-08-30 13:52:30
你做到了
df['bins'].astype(str).str.replace(']', ' ')
df['bins'].astype(str).str.replace(',', ' - ')但是.str.replace在内部不工作,您应该重新分配返回的内容,否则不会对您的pandas.DataFrame做任何更改,这是一个简单的例子:
import pandas as pd
df = pd.DataFrame({'col1':[100,200,300]})
df['col1'].astype(str).str.replace('100','1000')
print(df) # there is still 100
df['col1'] = df['col1'].astype(str).str.replace('100','1000')
print(df) # now there is 1000 rather than 100https://stackoverflow.com/questions/68985142
复制相似问题