1

I am new to pandas, and I have a dataframe (which I get after processing) that looks like this:
this

I was unable to find any help/info regarding how can I remove the first column (row_number/ index) to make it like this:
this

Any help would be appreciated.

Edit: additional question example:

third

11
  • 2
    If you're talking about the index, you cannot remove it, just prevent exporting it (e.g. as csv or xlsx, use index=False during export) Commented Oct 17, 2022 at 14:58
  • yes, was talking about index.. thanks, for the info Commented Oct 17, 2022 at 14:58
  • 1
    you can also use reset_index() to reset the index to be your row column if you desire. Commented Oct 17, 2022 at 14:59
  • by the way, can I also remove the column name? Commented Oct 17, 2022 at 14:59
  • All columns must have names. What column are you speaking of? Commented Oct 17, 2022 at 15:00

2 Answers 2

1

If I understand everything correctly then your dataframe looks like this:

   row   fruit  color
0    0   apple    red
1    1   berry   blue
2    2  grapes  green

Now if you want to write dataframe to a csv file without first column indexing then use dataframe.to_csv("file.csv", index=False) so that the file.csv file will look like this:

row,fruit,color
0,apple,red
1,berry,blue
2,grapes,green

And to remove header or first row use header=False to dataframe.to_csv("file.csv", index=False, header=False). Then your file.csv will look like:

0,apple,red
1,berry,blue
2,grapes,green

Hope this helps!!! And keep up the work toward learning Pandas.

Sign up to request clarification or add additional context in comments.

Comments

1

You can turn 'row' into your index, and remove the existing ("autonumbered" so to speak) index: df.set_index("row", inplace=True)

You can see all the options for set_index in the pandas docs:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.