JamesHudson81
JamesHudson81

Reputation: 2273

how can I turn a number into the value of a new dataframe?

I am trying to turn a scalar into a dataframe structure assigning a column name and an index name.

I have the following scalar (in concrete a numpy.float64) : -0.090058

and I would like to turn it into a df:

       decimal
ratio  -0.090058

I thought it was going to be straight forward. This is what I have tried unsuccesfully:

df=pd.DataFrame(value,index='ratio',columns='decimal') 

Upvotes: 2

Views: 31

Answers (2)

jezrael
jezrael

Reputation: 863301

Solution with passing dict:

df = pd.DataFrame({'decimal':value},index=['ratio'])
print (df)
        decimal
ratio -0.090058

Upvotes: 2

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210932

you were almost there:

In [222]: pd.DataFrame(value,index=['ratio'],columns=['decimal'])
Out[222]:
        decimal
ratio -0.090058

you can also do it this way:

In [223]: pd.DataFrame(index=['ratio']).assign(decimal=value)
Out[223]:
        decimal
ratio -0.090058

Upvotes: 2

Related Questions