Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable index pandas data frame

Tags:

python

pandas

How can I drop or disable the indices in a pandas Data Frame?

I am learning the pandas from the book "python for data analysis" and I already know I can use the dataframe.drop to drop one column or one row. But I did not find anything about disabling the all the indices in place.

like image 379
GeauxEric Avatar asked Aug 17 '13 15:08

GeauxEric


People also ask

How do I turn off indexing in pandas?

Remove Index of a Pandas DataFrame Using the reset_index() Method. The pandas. DataFrame. reset_index() method will reset the index of the DataFrame to the default index.

Can I remove the index from a Pandas Dataframe?

The most straightforward way to drop a Pandas dataframe index is to use the Pandas . reset_index() method. By default, the method will only reset the index, forcing values from 0 - len(df)-1 as the index. The method will also simply insert the dataframe index into a column in the dataframe.

How do I ignore index in Python?

Dealing with index and axis If you want the concatenation to ignore existing indices, you can set the argument ignore_index=True . Then, the resulting DataFrame index will be labeled with 0 , …, n-1 . To concatenate DataFrames horizontally along the axis 1 , you can set the argument axis=1 .


3 Answers

df.values gives you the raw NumPy ndarray without the indexes.

>>> df    x   y 0  4  GE 1  1  RE 2  1  AE 3  4  CD >>> df.values array([[4, 'GE'],        [1, 'RE'],        [1, 'AE'],        [4, 'CD']], dtype=object) 

You cannot have a DataFrame without the indexes, they are the whole point of the DataFrame :)

But just to be clear, this operation is not inplace:

>>> df.values is df.values False 

DataFrame keeps the data in two dimensional arrays grouped by type, so when you want the whole data frame it will have to find the LCD of all the dtypes and construct a 2D array of that type.

To instantiate a new data frame with the values from the old one, just pass the old DataFrame to the new ones constructor and no data will be copied the same data structures will be reused:

>>> df1 = pd.DataFrame([[1, 2], [3, 4]]) >>> df2 = pd.DataFrame(df1) >>> df2.iloc[0,0] = 42 >>> df1     0  1 0  42  2 1   3  4 

But you can explicitly specify the copy parameter:

>>> df1 = pd.DataFrame([[1, 2], [3, 4]]) >>> df2 = pd.DataFrame(df1, copy=True) >>> df2.iloc[0,0] = 42 >>> df1    0  1 0  1  2 1  3  4 
like image 177
Viktor Kerkez Avatar answered Sep 18 '22 01:09

Viktor Kerkez


d.index = range(len(d)) 

does a simple in-place index reset - i.e. it removes all of the existing indices, and adds a basic integer one, which is the most basic index type a pandas Dataframe can have.

like image 29
naught101 Avatar answered Sep 20 '22 01:09

naught101


Additionally, if you are using the df.to_excel function of a pd.ExcelWriter, which is where it is written to an Excel worksheet, you can specify index=False in your parameters there.

create the Excel writer:

writer = pd.ExcelWriter(type_box + '-rules_output-' + date_string + '.xlsx',engine='xlsxwriter')  

We have a list called lines:

# create a dataframe called 'df'
df = pd.DataFrame([sub.split(",") for sub in lines], columns=["Rule", "Device", "Status"]))

#convert df to Excel worksheet
df.to_excel(writer, sheet_name='all_status',**index=False**)
writer.save()
like image 24
Jason Sprong Avatar answered Sep 22 '22 01:09

Jason Sprong