Search and replace dots and commas in pandas dataframe

后端 未结 3 1898
情歌与酒
情歌与酒 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:03

    You can try

    df = df.apply(lambda x: x.replace(',', '&'))
    df = df.apply(lambda x: x.replace('.', ','))
    df = df.apply(lambda x: x.replace('&', '.'))
    
    0 讨论(0)
  • 2020-12-20 18:07

    You are always better off using standard system facilities where they exist. Knowing that some locales use commas and decimal points differently I could not believe that Pandas would not use the formats of the locale.

    Sure enough a quick search revealed this gist, which explains how to make use of locales to convert strings to numbers. In essence you need to import locale and after you've built the dataframe call locale.setlocale to establish a locale that uses commas as decimal points and periods for separators, then apply the dataframe's applymapp method.

    0 讨论(0)
  • 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))
    
    0 讨论(0)
提交回复
热议问题