0

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!

1 Answer 1

1

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
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.