Kelly Roper Kelly Roper - 1 month ago 7
Python Question

How to index the first column which has no header?

I have the following data frame for example. What I am trying to do is get the first column to be the index. When I try to do this, it doesn't work. When I am able to set the index successfully, I get a bunch of NaN's. Thank you!

0 1 2
0 842 Bill Smith
1 841 John Fisher
2 843 Barb Macdonald

Answer

pandas.DataFrame.set_index

Assuming your dataframe is variable df:

# not in-place
df = df.set_index(0)

# in-place
df.set_index(0, inplace=True)