user14578880
user14578880

Reputation:

Not able to display the column of a dataframe

When I am trying to print a single column of my data set it is showing errors

KeyError Traceback (most recent call last) ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2645 try: -> 2646 return self._engine.get_loc(key) 2647 except KeyError:

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Label'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last) in ----> 1 data['Label']

~\anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key) 2798 if self.columns.nlevels > 1: 2799 return self._getitem_multilevel(key) -> 2800 indexer = self.columns.get_loc(key) 2801 if is_integer(indexer): 2802 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2646 return self._engine.get_loc(key) 2647 except KeyError: -> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2649
indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2650 if indexer.ndim > 1 or indexer.size > 1:

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Label'

data['Label']

I have successfully read the csv and there exists a column named label idk why it is not able to read it. I am attaching a picture as well.

Upvotes: 0

Views: 24557

Answers (3)

ElFailali
ElFailali

Reputation: 11

You can use data.index if you want to print the Label column.

Upvotes: 0

Swapnal Shahil
Swapnal Shahil

Reputation: 84

If you have DataFrame and would like to access or select a specific few rows/columns from that DataFrame, you can use square brackets.

Now suppose that you want to select a column from the data(as per your question) DataFrame.

data["Label"]

But if you are unaware of the columns. You can get a column list and then display column data.

columns = data.columns.values.tolist()
data[columns[index]]

Upvotes: 0

Asrst
Asrst

Reputation: 169

It could be possible that the column name is having trailing spaces. Just try to print the column names & verify.

print(data.columns)

or try to print the columns after

data.columns = data.columns.str.strip()

Upvotes: 0

Related Questions