8-Bit Borges
8-Bit Borges

Reputation: 10033

Pandas adding row at index 0

I have a dataframe df with one row, like so:

               Player      Team  Points  Mean  Price  Value
Round                                                              
1              Salah  Liverpool     4.5    4.5   6.89  1.89

Now I need to add a dummy column at index 0 with an open price 'Price' = 5.0, obtained by df['Price'] - df['Value'], and all other column values set to 0.0, ending up with, like so:

               Player      Team  Points  Mean  Price  Value
Round
0              Salah  Liverpool     0.0    0.0   5.0    0.0                        
1              Salah  Liverpool     4.5    4.5   6.89  1.89

atleta_data.loc[-1] = [df['Player'].item(), 
                       df['Team'].item(),
                       0.0, 
                       0.0, 
                       (df['Price'].item() - df['Value'].item()), 
                       0.0]  
atleta_data.index = atleta_data.index +1  # shifting index
atleta_data = atleta_data.sort_index()  # sorting by index

But I'm getting Rounds 0 and 2.

              Player      Team  Points  Mean  Price  Value
Round
0              Salah  Liverpool     0.0    0.0   5.0    0.0                        
2              Salah  Liverpool     4.5    4.5   6.89  1.89

How can I end up with 0 and 1 for Rounds?

Upvotes: 0

Views: 68

Answers (2)

BENY
BENY

Reputation: 323226

Let us try

l = [df['Player'].item(), 
                       df['Team'].item(),
                       0.0, 
                       0.0, 
                       (df['Price'].item() - df['Value'].item()), 
                       0.0]

pd.DataFrame([l],columns=df.columns,index=[0]).append(df)
Out[127]: 
  Player       Team  Points  Mean  Price  Value
0  Salah  Liverpool     0.0   0.0   5.00   0.00
1  Salah  Liverpool     4.5   4.5   6.89   1.89

Upvotes: 1

azro
azro

Reputation: 54148

You may use .reset_index()

atleta_data = atleta_data.sort_index().reset_index()

Upvotes: 2

Related Questions