1

Quite new into pandas, I have generated a new column from division of last_value /first_value row in a this df.

df=
     data1 data2 
A      4    3
B      4    3
C      5    3
D      1    9

I generated this new column (with no headers)

new_column= df.loc[D]/df.loc[A]

new_column=
data1   0.25
data2   3

I have tried to add a header 'results' through :

new_column.rename(columns={0 :'results'}, inplace=True)
new_column.rename(columns={'0' :'results'}, inplace=True)
new_column['results']= df.loc[D]/df.loc[A]


print (new_column)

From the research done, nothing seems to work/adapt:

Adding Column Headers to new pandas dataframe

The result is the same without any headers.How can I include the header?

What I am looking for is :

       results
data1   0.25
data2   3

In short the new_column with the header 'results'

Community
  • 1
  • 1
JamesHudson81
  • 2,215
  • 4
  • 23
  • 42

2 Answers2

4

new_column is a pd.Series, it doesn't have "columns", it does however have a name:

>>> new_column = df.loc['D']/df.loc['A']
>>> new_column
data1    0.25
data2    3.00
dtype: float64

By default, it doesn't have one if constructed this way, but:

>>> new_column.rename('results', inplace=True)
data1    0.25
data2    3.00
Name: results, dtype: float64

You want a pandas.DataFrame, so you could this instead of the above:

>>> new_column = (df.loc['D']/df.loc['A']).to_frame()
>>> new_column
          0
data1  0.25
data2  3.00

And now your attempts work:

>>> new_column.rename(columns={0 :'results'}, inplace=True)
>>> new_column
       results
data1     0.25
data2     3.00

Or, you could have done it in one, fell, swoop:

>>> new_column = pd.DataFrame((df.loc['D']/df.loc['A']), columns=['results'])
>>> new_column
       results
data1     0.25
data2     3.00

Edit to add suggestion by @MaxU

Or perhaps the easiest way is as suggested by MaxU:

>>> (df.loc['D']/df.loc['A']).to_frame('results')
       results
data1     0.25
data2     3.00
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
3

I'm editing this on my phone, so excuse the funky formatting, but does this do it?

new = pd.DataFrame({'results':df.loc[D]/df.loc[A]}) 
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
elPastor
  • 8,435
  • 11
  • 53
  • 81