Search and replace dots and commas in pandas dataframe

后端 未结 3 1910
情歌与酒
情歌与酒 2020-12-20 17:35

This is my DataFrame:

d = {\'col1\': [\'sku 1.1\', \'sku 1.2\', \'sku 1.3\'], \'col2\': [\'9.876.543,21\', 654, \'321,01\']}
df = pd.DataFrame(data=d)
df

           


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 18:24

    The best is use if possible parameters in read_csv:

    df = pd.read_csv(file, thousands='.', decimal=',')
    

    If not possible, then replace should help:

    df['col2'] = (df['col2'].replace('\.','', regex=True)
                            .replace(',','.', regex=True)
                            .astype(float))
    

提交回复
热议问题