0

I'm not entirely sure how to go about solving problems, but here is one I'm encountering:

for row in concat_data.index: 
  if (concat_data['% interest'][row]).str.contains('%'):
    concat_data['% interest'] = concat_data['% interest'].str.split('%', expand=True)

concat_data is my dataframe, I would like to iterate through each row of the '% interest' column and split at '%' if it is found in that cell.

The error message is:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I have tried using .any(), but it also hasn't worked.

I added a print(concat_data['% interest'][row]) and it says that it's a Series.

Any help would be appreciated it, thank you!

ti7
  • 16,375
  • 6
  • 40
  • 68
  • You cannot set a value to multiple values. You can put an orange into a bucket, but you cannot put a bucket into an orange... For example in excel you cannot set Cell `A1` to the entire column `A` or a slice of column A, let's sat `A5:A10` to cell `A1`. In terms of your stackoverflow question, kindly format your code, include sample input data and sample expected output data. Eseentially, you to create a minimum reproducible example. Please see: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – David Erickson Oct 20 '20 at 20:56
  • `if` operates on a single boolean value, but a `pd.Series` represents multiple values. You need a vectorized boolean mask operation like [`np.where`](https://numpy.org/doc/stable/reference/generated/numpy.where.html). – 0x5453 Oct 20 '20 at 20:57

3 Answers3

0

Here are some options... If you want to do something on every row of your dataframe which match your condition, you can:

concat_data = pd.DataFrame(data=['5.3','5.3%','5.3%APR'],columns=['% interest'])
for row in concat_data.index:
    if '%' in concat_data['% interest'][row]:
        concat_data['% interest'][row] = concat_data['% interest'][row].split('%')

This uses the 'if' statement correctly as it is only operating on one value at a time.

Or you can update the column using list comprehension:

concat_data['% interest'] = [x.split('%') if '%' in x else x for x in concat_data['% interest']]
Dharman
  • 30,962
  • 25
  • 85
  • 135
Douglas Daly
  • 85
  • 10
  • Thank you for your response. I actually used that if statement previously, but it gives me this error: argument of type 'float' is not iterable – TryingtoCode Oct 20 '20 at 21:28
0

I don't think you need to use the if statement, nor the iteration through the dataframe at all. The split method will take everything before the first occurrence of a '%' and leave the value untouched if it doesn't contain a '%' in the first place.

So basically just do this:

concat_data['% interest'] = concat_data['% interest'].str.split('%', expand=True)

That is assuming your data looks like this '50.6%'. It would be helpful to see a sample of your data.

nagini
  • 11
  • 2
  • Thank you for your response. That was my initial code, but it actually empties each cell that doesn't have a %. That's why I added the if statement. – TryingtoCode Oct 20 '20 at 21:27
0

data

This is what that column looks like, my code should ignore cells like "0.005" and split cells like " 7.75% (1M L+675, 1.00%" into "7.75%".

Thanks again.