df
| Product | Price | Tax |
|---|---|---|
| Fork | U$ 14,00 | U$ 0,40 |
| Knife | U$ 23,00 | U$ 0,85 |
| Knife | U$ 23,00 | U$ 0,91 |
| Knife | U$ 23,00 | U$ 0,77 |
df_sum_price = df['Price'].sum()
df_sum_tax = df['Tax'].sum()
df_sum_price
df_sum_tax
output wanted:
>>> U$ 83,00
U$ 2,93
HOW CAN I DO THIS? Because i need before convert Object to Int or Float but i don't know why! .-. help me please
CodePudding user response:
To my knowledge, there is no currency type in Python. I guess your "Price" column is a string? The first operation would be to turn this column into a float. To do so, you have to remove the "U$ " in front of the digits, and then turn the "," into "." to be able to turn your string into a float. So a function doing that would be:
def price_to_float(price):
return(float(price[3:].replace(',','.')))`
Then, just apply this function to your whole column with:
df['Price']=df.apply(lambda row : price_to_float(row['Price']), axis = 1)
df['Tax']=df.apply(lambda row : price_to_float(row['Price']), axis = 1)
Now you can perform all the operations you want to! You can then turn your column back to a currency string if you need to.
CodePudding user response:
if you have an option to convert datatype in dataframe the you can do this
df['Tax']=(df['Tax'].str[3:]).str.replace(',','').astype(int)
df['Price']=(df['Price'].str[3:]).str.replace(',','').astype(int)
output:
Product Price Tax
0 Fork 1400 40
1 Knife 2300 85
2 Knife 2300 91
3 Knife 2300 77
df_sum_price = df['Price'].sum()
df_sum_tax = df['Tax'].sum()
df_sum_price
df_sum_tax
output:
8300
293
if you dont have the freedom to change datatype and just want to perform the sum then do this:
df_sum_price = (df['Tax'].str[3:]).str.replace(',','').astype(int).sum()
df_sum_tax = (df['Price'].str[3:]).str.replace(',','').astype(int).sum()
df_sum_price
df_sum_tax
output is same in both the cases.
