python: extract value from one dataframe and insert into another

Question:

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?

Asked By: keenan

||

Answers:

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]
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.