[英]pandas.Dataframe.loc() and pandas.Dataframe.drop() not working
I am using pandas to create a ranklist.我正在使用 pandas 创建一个排名列表。 I created a csv file and used pandas to create a Dataframe.
我创建了一个 csv 文件并使用 pandas 创建了一个 Dataframe。 When I am slicing the dataframe using iloc method, its working fine, but its showing error with loc method.
当我使用 iloc 方法对 dataframe 进行切片时,它工作正常,但使用 loc 方法显示错误。 Similar error is also shown with drop method.
使用 drop 方法也会显示类似的错误。 When I am dropping the first column "Name", it works fine, but it again doesn't work with the remaining columns.
当我删除第一列“名称”时,它工作正常,但它再次不适用于其余列。
import pandas
#creating CRL
file1 = pandas.read_csv("csvfile_of_ranklist2.csv")
print(file1.index)
print(file1.columns)
file2 = file1.drop("Name",axis=1)
print(file1)
print(file2)
It gave the following output:它给出了以下 output:
C:\Users\VIVEK PANDYA\Documents\Python programs>E:/python.exe "c:/Users/VIVEK PANDYA/Documents/Python programs/ranklist.py"
RangeIndex(start=0, stop=8, step=1)
Index(['Name', ' Category', ' Maths', ' Physics', ' Chemistry', 'Unnamed: 5'], dtype='object')
Name Category Maths Physics Chemistry Unnamed: 5
0 Vivek Pandey C1 89 78 100 NaN
1 Prateek Singh C3 100 75 68
2 Yuvraj Singh C3 88 92 36 NaN
3 Shaiqua Parveen C2 100 52 32 NaN
4 Sana Afshan C1 78 85 21 NaN
5 Shabber Islam C2 89 23 56 NaN
6 Brahma Pandey C4 78 54 62 NaN
7 Santosh Singh C1 85 64 92 NaN
Category Maths Physics Chemistry Unnamed: 5
0 C1 89 78 100 NaN
1 C3 100 75 68
2 C3 88 92 36 NaN
3 C2 100 52 32 NaN
4 C1 78 85 21 NaN
5 C2 89 23 56 NaN
6 C4 78 54 62 NaN
7 C1 85 64 92 NaN
However if I try to drop "Category", it produces error:但是,如果我尝试删除“类别”,则会产生错误:
import pandas
#creating CRL
file1 = pandas.read_csv("csvfile_of_ranklist2.csv")
print(file1.index)
print(file1.columns)
file2 = file1.drop("Category",axis=1)
print(file1)
print(file2)
C:\Users\VIVEK PANDYA\Documents\Python programs>E:/python.exe "c:/Users/VIVEK PANDYA/Documents/Python programs/ranklist.py"
RangeIndex(start=0, stop=8, step=1)
Index(['Name', ' Category', ' Maths', ' Physics', ' Chemistry', 'Unnamed: 5'], dtype='object')
Traceback (most recent call last):
File "c:\Users\VIVEK PANDYA\Documents\Python programs\ranklist.py", line 8, in <module>
file2 = file1.drop("Category",axis=1)
File "E:\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "E:\lib\site-packages\pandas\core\frame.py", line 4906, in drop
return super().drop(
File "E:\lib\site-packages\pandas\core\generic.py", line 4150, in drop
obj = obj._drop_axis(labels, axis, level=level, errors=errors)
File "E:\lib\site-packages\pandas\core\generic.py", line 4185, in _drop_axis
new_axis = axis.drop(labels, errors=errors)
File "E:\lib\site-packages\pandas\core\indexes\base.py", line 6017, in drop
raise KeyError(f"{labels[mask]} not found in axis")
KeyError: "['Category'] not found in axis"
Similarly, if i try to drop the indexes or slice indexes or columns using labels, it produces 'index not found in axis' or 'column not found in axis' error.同样,如果我尝试使用标签删除索引或切片索引或列,则会产生“轴中未找到索引”或“轴中未找到列”错误。 I tried looking for any solution in pandas documentation, but I found no solution.
我尝试在 pandas 文档中寻找任何解决方案,但没有找到解决方案。 How can this be solved?
如何解决?
Get column names by file1.columns
.通过
file1.columns
获取列名。 Copy and use these column names.复制并使用这些列名。 Your column names probably have whitespaces in the beginning or in the end.
您的列名可能在开头或结尾有空格。
' Category'
and most of the other column names have a leading whitespace. ' Category'
和大多数其他列名都有一个前导空格。 It says in the error KeyError: "['Category'] not found in axis"
, because 'Category'
isn't a columnname.它在错误
KeyError: "['Category'] not found in axis"
,因为'Category'
不是列名。
A simple way to tidy up your column names is:整理列名的简单方法是:
file.columns = [x.strip() for x in file.columns]
Now your Pandas Dataframe has columns without the leading whitespace and using 'Category'
does work.现在您的 Pandas Dataframe 的列没有前导空格,并且使用
'Category'
确实有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.