Home > Enterprise >  Converting returned values from a function into a data frame
Converting returned values from a function into a data frame

Time:01-30

I have a function that works as expected and outputs 4 values

targets(0.0034) #calling the function
(1014.0, 260, 176, 84)

All I want to do is to put these values into a data frame so it looks like this

   value 1   value 2  value 3   value 4
0   1014      260       176       84

I have tried to make a new dataframe new = pd.DataFrame(columns=['value1','value2','value3','value4']) then tried to append in different ways but keep getting stuck. I Have tried to reassign the values but everything I have tried seems to be a dead end.

What am I missing? Thanks!

CodePudding user response:

There are a lot of different ways to do it, but with a single tuple entered as the data param for DataFrame we get 4 rows. So we can use .T to transpose the data and get four columns and one row. We can then rename the columns.

def targets():
    
    return (1014.0, 260, 176, 84)

df = pd.DataFrame(targets()).T.rename({0:'value 1', 1:'value 2', 2:'value 3', 3:'value 4'}, axis='columns')

print(df)

    value 1  value 2  value 3   value 4
0   1014.0   260.0    176.0     84.0
  •  Tags:  
  • Related