Michael Norman
Michael Norman

Reputation: 139

Python, manipulating dataframes

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

Upvotes: 0

Views: 47

Answers (1)

erratic_strategist
erratic_strategist

Reputation: 91

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.

Upvotes: 1

Related Questions