Department = input("what dept")
editfile = pd.read_csv('52.csv', encoding='Latin-1')
editfilevalues= editfile.loc[editfile['Customer'].str.contains(Department, na=False), 'May-18\nQty']
editfilevalues = editfilevalues.fillna(int(0))
print(int(editfilevalues) *1.3)
I have looked through stackoverflow and no answer seems to help me this problem. I simply want to be able to manipulate data in a series like this but I get different errors, with this current code I receive this:
"{0}".format(str(converter))) TypeError: cannot convert the series to <class 'int'>
My main issue is converting a series to an int type, I have tried several different ways to do this and none are giving me the results
-
Why are you trying to convert Series to int?erratic_strategist– erratic_strategist2018年07月17日 17:22:32 +00:00Commented Jul 17, 2018 at 17:22
-
@erratic_strategist I am trying to take the values from this series and manipulate the data. That is why I was trying to multiply it by 1.3Michael Norman– Michael Norman2018年07月17日 17:31:23 +00:00Commented Jul 17, 2018 at 17:31
-
Also is there a reason I got a downvote for this question? What is wrong with this. Please let me know so I can ask questions better in the futureMichael Norman– Michael Norman2018年07月17日 18:33:23 +00:00Commented Jul 17, 2018 at 18:33
-
1I didn't submit the downvote, but it could have been due to formatting, which I've since edited. The formatting can aid in conveying the information effectively. Particularly, the error message did not stand out separately from the text and was tough to decypher.bzier– bzier2018年07月17日 18:37:59 +00:00Commented Jul 17, 2018 at 18:37
-
1@erratic_strategist Okay that makes sense, and it is hard for me because I tried to look it up but could not find an answer, and I am new to coding in generalMichael Norman– Michael Norman2018年07月17日 18:39:32 +00:00Commented Jul 17, 2018 at 18:39
1 Answer 1
So a pandas Series is a bit like a list, but with different functions and properties. You can't convert the Series to int using int() because the function wasn't designed to work on list-like objects in that way.
If you need to convert the Series to all integers, this method will work.
int_series = your_series.astype(int)
This will get your entire series as 'int32' specifically. Below is a bonus if you want it in a numpy array.
int_array = your_series.values.astype(int)
From here you have a few options to do your calculation.
# where x is a value in your series and lambda is a nameless function
calculated_series = int_series.apply(lambda x: some_number*x)
The output will be another Series object with your rows calculated. Bonus using numpy array below.
calculated_array = int_array * some_number
Edit to show everything at once.
# for series
int_series = your_series.astype(int)
calculated_series = int_series.apply(lambda x: x * some_number)
# for np.array
int_array = your_series.values.astype(int)
calculated_array = int_array * some_number
Either will work, and it is ultimately up to what kind of data structure you want at the end of it all.