Home > Software design >  python: extract value from one dataframe and insert into another
python: extract value from one dataframe and insert into another

Time:09-29

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?

CodePudding user response:

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]
  • Related