Home > other >  Python dataframe: how to remove first column (row number)
Python dataframe: how to remove first column (row number)

Time:11-01

I have manually constructed the dataframe (df1,code below). For this dataframe df1, how could I remove the first column (row number)? Many thanks in advance

df1 = pd.DataFrame({"date": ['2021-3-22', '2021-4-7', '2021-4-18', '2021-5-12'],
"x": [3, 3, 3, 0 ]})

Output:
date    x
0   2021-3-22   3
1   2021-4-7    3
2   2021-4-18   3
3   2021-5-12   0

CodePudding user response:

Simply use set_index method.

import pandas as pd

df1 = pd.DataFrame({"date": ['2021-3-22', '2021-4-7', '2021-4-18', '2021-5-12'],
"x": [3, 3, 3, 0 ]})

df1.set_index('date', inplace=True)

CodePudding user response:

The default dataframe representation will display index numbers by default. If you want to see your dataframe without index you could use .to_string method with index=False.

>>> df1
        date  x
0  2021-3-22  3
1   2021-4-7  3
2  2021-4-18  3
3  2021-5-12  0
>>> 
>>> print(df1.to_string(index=False))
     date  x
2021-3-22  3
 2021-4-7  3
2021-4-18  3
2021-5-12  0
  • Related