Search code examples
pythonpandaspivot-tabledelete-row

Delete row in pandas DataFrame


I have got this table:

Trader  Date        EUR
T1      01.05.2020. 12.36
T2      03.05.2020. 14.48
T2      02.05.2020. 23.69
T1      04.05.2020. 45.78
T3      04.05.2020. 15.26

After I apply pivot in Python by using pandas library:

a = a.pivot_table(index=['Date'],columns=['Trader'],values=['EUR'])

I got this view:

                                 EUR
    Trader         T1      T2     T3
      Date          
01.05.2020.     12.36     NaN    NaN
02.05.2020.       NaN   23.69    NaN
03.05.2020.       NaN   14.48    NaN
04.05.2020.     45.78     NaN  15.26

Now I would like to get rid of first row (EUR) and third row (Date) and use the rest later. In Excel I would simply delete both rows. How to do this in Python/panda?

Thanks

Valters


Solution

  • Remove one element lists from your solution:

    a = a.pivot_table(index='Date',columns='Trader',values='EUR')
    

    And then if need column from index Date then use DataFrame.rename_axis and DataFrame.reset_index:

    a = (a.pivot_table(index='Date',columns='Trader',values='EUR')
          .rename_axis(None, axis=1)
          .reset_index())