pandas: convert the "object" type.



  • It's necessary to calculate the MMR, but in the dayset, the necessary "Spend's" column is relevant.

    import pandas as pd
    

    df=pd.read_csv('test_data.csv',sep=',')

    введите сюда описание изображения

    metrics_cpm = df.groupby('Buyer', as_index= False).agg({'Spend': 'sum', 'Impressions': 'sum'})metrics_cpm

    введите сюда описание изображения

    metrics_cpm['Cost Per Mille'] = round(metrics_cpm['Spend']/metrics_cpm['Impressions']*1000, 5)metrics_cpm

    введите сюда описание изображения

    Trying to change the pole type with a team:

    df['Spend'] = df['Spend'].str.replace(',','.','').astype(np.float64)

    But the mistake remains, how do I make the calculation?



  • Add the parameter decimal=',' challenge pd.read_csvthat's better than doing. replace I don't know. Of course, it's a strange situation where you and the separator of the columns and the division of the grades have the same symbol. ,but, in principle, this may be, in this case, the contents of the columns come in quotes and Pandas I can figure out where.

    Either if you want to change manually, you can do it. .str.replace(',','.')And your option. .str.replace(',','.','') Changed everything into an empty line, which eventually became meaning. NA after change float



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2