GeauxEric GeauxEric - 1 year ago 134
Python Question

disable index pandas data frame

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.

Answer Source

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

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download