Python Pandas:Dataframe.Drop - ValueError:标签 ['id'] 未包含在轴中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42116091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:15:36  来源:igfitidea点击:

Pandas: Dataframe.Drop - ValueError: labels ['id'] not contained in axis

pythonpandas

提问by saar

Attempting to drop a column from a DataFramein Pandas. DataFramecreated from a text file.

试图从DataFramePandas 中的a中删除一列。 DataFrame从文本文件创建。

import pandas as pd 
df = pd.read_csv('sample.txt')
df.drop(['a'], 1, inplace=True)

However, this generates the following error:

但是,这会产生以下错误:

ValueError: labels ['a'] not contained in axis      

Here is a copy of the sample.txtfile :

这是sample.txt文件的副本:

a,b,c,d,e
1,2,3,4,5
2,3,4,5,6
3,4,5,6,7
4,5,6,7,8

Thanks in advance.

提前致谢。

回答by Rob Davis

So the issue is that your "sample.txt" file doesn't actually include the data you are trying to remove.

所以问题是您的“sample.txt”文件实际上并不包含您要删除的数据。

Your line

你的线路

df.drop(['id'], 1, inplace=True) 

is attepmting to take your DataFrame (which includes the data from your sample file), find the column where the value is 'id' in the first row (axis 1) and do an inplace replace (modify the existing object rather than create a new object missing that column, this will return None and just modify the existing object.).

正在尝试获取您的 DataFrame(其中包括您的示例文件中的数据),在第一行(轴 1)中找到值为“id”的列并进行就地替换(修改现有对象而不是创建一个新对象)对象缺少该列,这将返回 None 并只修改现有对象。)。

The issue is that your sample data doesn't include a column with a header equal to 'id'.

问题是您的示例数据不包含标题等于“id”的列。

In your current sample file, you can only to a drop where the value in axis 1 is 'a', 'b', 'c', 'd', or 'e'. Either correct your code to drop one of those values or get a sample files with the correct header.

在您当前的示例文件中,您只能删除轴 1 中的值为“a”、“b”、“c”、“d”或“e”的放置。要么更正您的代码以删除这些值之一,要么获取带有正确标题的示例文件。

The documentation for Pandas isn't fantastic, but here is a good example of how to do a column drop in Pandas: http://chrisalbon.com/python/pandas_dropping_column_and_rows.html

Pandas 的文档不是很好,但这里是如何在 Pandas 中进行列删除的一个很好的例子:http: //chrisalbon.com/python/pandas_dropping_column_and_rows.html

** Below added in response to Answer Comment from @saar

** 以下添加以回应@saar 的回答评论

Here is my example code: Sample.txt:

这是我的示例代码:Sample.txt:

a,b,c,d,e
1,2,3,4,5
2,3,4,5,6
3,4,5,6,7
4,5,6,7,8

Sample Code:

示例代码:

import pandas as pd

df = pd.read_csv('sample.txt')
print('Current DataFrame:')
print(df)
df.drop(['a'], 1, inplace=True)
print('\nModified DataFrame:')
print(df)

Output:

输出:

>>python panda_test.py
Current DataFrame:
   a  b  c  d  e
0  1  2  3  4  5
1  2  3  4  5  6
2  3  4  5  6  7
3  4  5  6  7  8

Modified DataFrame:
   b  c  d  e
0  2  3  4  5
1  3  4  5  6
2  4  5  6  7
3  5  6  7  8

回答by user8423842

bad= pd.read_csv('bad_modified.csv')
A=bad.sample(n=10)
B=bad.drop(A.index,axis=0)

This is an example of dropping a dataframe partly. In case you need it.

这是部分丢弃数据帧的示例。万一你需要它。