disable index pandas data frame

前端 未结 5 1518
既然无缘
既然无缘 2021-02-05 05:04

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 datafr

5条回答
  •  猫巷女王i
    2021-02-05 05:27

    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
    

提交回复
热议问题