last day (24 days later) » 

9:18 PM
0
A: Pandas merge column duplicate and sum value

Celius StingherI believe there must be empty spaces in the example you are giving and that's the reason for why astype(int) isn't working. Are you dealing with other values than the ones you've shared in your example? import pandas as pd a = {'quantity':[1,3,1,2],'color':['White','White','Black','Black']} df =...

 
No, just two columns (quantity, color).
 
I mean in the quantity column, are other values? Did you tried .str.replace(' ', '').astype(int)? if it's not working, what error are you getting? Could there values that have commas ?
 
There are no other values other than numbers in the quantity column. Yes, I tried the replace blank spaces code, but still getting "ValueError: invalid literal for int() with base 10: 'quantity'"
 
How about we use astype('float64')? Edited in answer too.
 
I just looked again at the csv i'm reading. Only thing in the quantity column are numbers 1, 2, 3, 4. No weird characters or anything.
It's trying to pass (float64) as a variable. Not defined.
 
9:18 PM
Sorry meant between apostrophes. astype('float64')
Is it possible that your csv contains empty values in the column quantity?
 
ValueError: could not convert string to float: 'quantity'
 
I moved this to discussion so we can chat freely
I have only 2 more tools left that I know could help try this
 
Hi there, thanks for your help. There are no empty values at all in the column quantity
 
My pleasure
Try with this now: df['quantity'] = pd.to_numeric(df['quantity'],errors='coerce')
This should work, if it doesn't then I'm pretty sure something is wrong when reading the csv
Let me know and I'll edit my answer too so it fits you question
 
That worked although I have no understanding as to why.
 
9:24 PM
My best guess is that you have empty values in your csv that when read are being passed as empty strings
Empty strings can't be converted to astype('float64'),int` or whatever, so the only way to get around those is to use pd.to_numeric with errors='coerce' so that when they are found errors=coerce automatically sets them to np.Nan. And that's how we find a way to get around the issue
 
I only had headings at the top quantity and color. I'm not sure if it was trying to pass those as values.
 
Oh, that could be a reason too, true
 
Nonetheless, thank you for your help! I appreciate it.
 
No problem the issue was simple
Once you tried to conver them to float
Because we found out that there was a string messing up our dtypes. It's tricky because converting to int yields a different error than when converting to float64
This is what made it clearer: "ValueError: could not convert string to float: 'quantity'"
Cheers, and I hope you can get your job done soon!
 

last day (24 days later) »