0

I have a pandas DataFrame like so:

      Col_A     Col_B
   0.   1         5
   1.   2         6
   2.   3         7
   3.   4         8

I'm trying to do this to pass a and b as variables inside a function I'm defining. For this, a should be a numpy array of the values in column A but I'm not being able to do that.

So far I've tried:

a = np.empty(1); a.fill(df[0])

But it returns:

ValueError: Input object to FillWithScalar is not a scalar

6
  • Typical XY problem. Describe your problem and what you try to do, not why your solution is not working. Commented Jul 9, 2019 at 23:15
  • @Erfan thank you for pointing this out, I didn't realise while I was writing the question. I think I've made it more clear now. My issue is that I'm not being able to return a numpy array from a column of a pandas DataFrame Commented Jul 9, 2019 at 23:23
  • Take a step back and 1. provide an example dataset (which you already did). 2. Explain what you try to achieve with your data (pass a and b as variables is not what I mean, thats still your solution). 3. Add an expected output based on your example dataset. Commented Jul 9, 2019 at 23:31
  • df['A'].to_numpy() or df['A'].values should return column as a 1d numpy array. Commented Jul 10, 2019 at 0:02
  • np.empty(1) creates an array with space for 1 float element. You can only fill it with a scalar. Commented Jul 10, 2019 at 0:04

1 Answer 1

1

You could take the column and turn it into a numpy array using the following:

a = np.array(df['Col_A'])

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

Comments

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.