列表理解python中的多个条件
我有一列价格,下面我想用Python清理一行列表列表理解python中的多个条件,python,regex,pandas,list-comprehension,Python,Regex,Pandas,List Comprehension,我有一列价格,下面我想用Python清理一行列表 Prices 15.90 EUR 17.80 EUR 15,80 EUR 26.10 EUR 44,10 EUR 3A'999,90,,,,,,,, 我的代码: prices = df.Prices prices = [re.findall('\d+.\d+',str(x).replace(',','.'))[0] for x in prices] # It works but not suitable for last price pri
Prices
15.90 EUR
17.80 EUR
15,80 EUR
26.10 EUR
44,10 EUR
3A'999,90,,,,,,,,
我的代码:
prices = df.Prices
prices = [re.findall('\d+.\d+',str(x).replace(',','.'))[0] for x in prices] # It works but not suitable for last price
prices = [x==re.findall('\d+.\d+',str(x).replace(',','.')) for x in prices if len(x)>0 else None] # Wrong syntax
我希望如果没有匹配,它应该添加None
,如果正则表达式匹配,则更正价格。我可以通过使用if-else或try-except来用很长的时间来完成,但我想使用一个班轮。可能吗 正确的语法
prices = [x==re.findall('\d+.\d+',str(x).replace(',','.')) if len(x)>0 else None for x in prices]
你为什么要用一个班轮?它看起来太密集了,你能发布你想要的结果数据集吗?
['15.90','17.80','15.80','26.10','44.10',无]
。这是我可以从这个问题中得到的结果。但是后来我想清理3A'999,90,,,,,,,,
到3999.00
但是我必须考虑正则表达式,因为我不太使用正则表达式。
In [35]: df
Out[35]:
Prices
0 15.90 EUR
1 17.80 EUR
2 15,80 EUR
3 26.10 EUR
4 44,10 EUR
5 3A'999,90
6 333
In [36]: df.dtypes
Out[36]:
Prices object
dtype: object
In [37]: df['Prices'] = pd.to_numeric(df.Prices.str.replace(',','.')
...: .str.extract(r'(\d+[\.,]{,1}?\d+?)',
...: expand=False),
...: errors='coerce')
In [38]: df
Out[38]:
Prices
0 15.9
1 17.8
2 15.8
3 26.1
4 44.1
5 999.9
6 333.0
In [39]: df.dtypes
Out[39]:
Prices float64
dtype: object