0

The issue is part of a much larger and more complex code but I have narrowed down the minimum reproducible behavior to the following

df1 = pd.DataFrame({'cola':[1,2,3], 'colb':['a','b','c']})

selected = df1[df1['cola'] == 1]

tempDf = pd.DataFrame({'col1':[selected['cola']], 'col2': [10]})

my expected output is a two column df with the values of 1 and 10. What I am getting is a two column df where the first column is 0 1 Name: cola, dtype: int64 and the second column is 10. How can I edit this code to only get the column value and what is causing this behavior?

1
  • tempDf = pd.DataFrame({'col1':selected['cola'], 'col2': [10]}) get rid of extra squared bracket. selected['cola'] itself is 1D values which you are putting inside a list that makes it include the indices as well. Commented Sep 29, 2022 at 3:47

1 Answer 1

1

What you are passing is a list of 1D values by [selected['cola']] for col1, if you remove the enclosing squared bracket, you'll get the expected output:

tempDf = pd.DataFrame({'col1':selected['cola'], 'col2': [10]})
# tempDf
   col1  col2
0     1    10

# for reference
# print([selected['cola']])
# [0    1
# Name: cola, dtype: int64]
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.