禁用索引 pandas 数据框 [英] disable index pandas data frame

查看:54
本文介绍了禁用索引 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除或禁用 Pandas 数据框中的索引?

我正在从python for data analysis"一书中学习熊猫,我已经知道我可以使用 dataframe.drop 删除一列或一行.但我没有找到任何关于禁用所有索引的信息.

解决方案

df.values 为您提供没有索引的原始 NumPy ndarray.

<预><代码>>>>dfxy0 4 通用电气1 1 RE2 1 AE3 4 光盘>>>df.values数组([[4, 'GE'],[1, 'RE'],[1, 'AE'],[4, 'CD']], dtype=object)

没有索引就不能拥有 DataFrame,它们是 DataFrame 的全部:)

但需要说明的是,这个操作不是就地:

<预><代码>>>>df.values 是 df.values错误的

DataFrame 将数据保存在按类型分组的二维数组中,因此当您想要整个数据框时,它必须找到所有 dtype 的 LCD 并构建该类型的二维数组.

要使用旧数据帧的值实例化新数据帧,只需将旧数据帧传递给新数据帧的构造函数,并且不会复制任何数据,相同的数据结构将被重用:

<预><代码>>>>df1 = pd.DataFrame([[1, 2], [3, 4]])>>>df2 = pd.DataFrame(df1)>>>df2.iloc[0,0] = 42>>>df10 10 42 21 3 4

但是你可以明确指定copy参数:

<预><代码>>>>df1 = pd.DataFrame([[1, 2], [3, 4]])>>>df2 = pd.DataFrame(df1, copy=True)>>>df2.iloc[0,0] = 42>>>df10 10 1 21 3 4

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.

解决方案

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

这篇关于禁用索引 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆