0
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

8
  • Why are you trying to convert Series to int? Commented 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.3 Commented 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 future Commented Jul 17, 2018 at 18:33
  • 1
    I 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. Commented 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 general Commented Jul 17, 2018 at 18:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Okay Thank you , however I am not getting the error AttributeError: 'numpy.ndarray' object has no attribute 'apply'
If you want to use the numpy array simply multiply the value like, np_array * some_number. You do not need to use apply in that case.
int_series = editfilevalues.values.astype(int) calculated_series = int_series.apply(lambda x: editfilevalues*1.3) Okay I took your advice and did it as so, simply multiplying by the value, but that is the error I recieve Maybe I am not grasping exactly what the code is doing
Replace the editfilevalues in the lambda expression with x. The editfilevalues is a series whereas the x is acting as the value. It should look like int_series.apply(lambda x: x * 1.3). Does that help?
I was able to make it work! I just removed the .value part. Thanks alot for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.