python - Cannot Calculate Sum of Currency-Based Column Data in Pandas -


i have following csv data:

+----------+-------------+-------+---------+ | category | part number | units |  cost   | +----------+-------------+-------+---------+ | axel     |          78 |   587 | $159.95 | | rim      |          48 |   234 | $38.75  | | nut      |          39 |  1234 | $0.15   | | axel     |          79 |    67 | $110.95 | +----------+-------------+-------+---------+ 

and following code:

# importing libraries import numpy np import matplotlib.pyplot plt import pandas pd  # importing dataset df = pd.read_csv('stock.csv',engine="python")  #sum of values category df.groupby('category').sum()['units'] df.groupby('category').sum()['cost'] 

when run second last line, following output:

df.groupby('category').sum()['units'] out[4]:  category axel     654 nut     1234 rim      234 name: units, dtype: int64 

when run last line, following error:

keyerror: 'cost' 

i'm not sure if there simple way sum data without converting data type integer , converting back.

.sum() ignores non-numeric columns. you've got convert cost numbers first:

df["cost"] = df["cost"].str[1:].astype(float) 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -