Python Forum
Change column names from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change column names from a file
#1
Hi All,

I want to change the column names to the name i have in the csv file. Below is the code i used but i am getting an error i.e.

Keyerror: 'Column_Names'

data_columns = pd.read_csv('E:/Personal/Learning/Predictive ModelingBook/Book Datasets/Customer Churn Columns.csv')
data_column_list = data_columns['Column_Names'].tolist()
data=pd.read_csv('E:/Personal/Learning/Predictive Modeling Book/BookDatasets/Customer Churn Model.txt',header=None,names=data_column_list)
data.columns.values
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             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: 'Column_Names'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-31-93082a685b38> in <module>
      2 
      3 data_columns = pd.read_csv('C:/Users/Nidhesh.Tiwari/Desktop/Udemy/Prediction with Python/Predictive Modelling/Customer Churn Model.csv')
----> 4 data_column_list = data_columns["Column_Names"].tolist()
      5 data=pd.read_csv('C:/Users/Nidhesh.Tiwari/Desktop/Udemy/Prediction with Python/Predictive Modelling/Customer Churn Model.txt',header=None,names=data_column_list)
      6 data.columns.values

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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: 'Column_Names'
Please help me in the same.

Thanks.
Reply
#2
data_columns is Pandas dataframe. It doesn't contain a column named Column_Names. Everything depends
on content of the file Customer Churn Columns.csv. Try to print all column names
after the data is loaded, e.g. print(data_columns.columns).
Reply
#3
(Jul-08-2019, 01:47 AM)scidam Wrote: data_columns is Pandas dataframe. It doesn't contain a column named Column_Names. Everything depends
on content of the file Customer Churn Columns.csv. Try to print all column names
after the data is loaded, e.g. print(data_columns.columns).

Thanks for your reply. but i am not getting how to store all the names of columns and use that column names to rename columns of another file.

Moreover, i tried the print command and use the 'Area Code' i.e.

import pandas as pd
import os
import numpy as np

data_columns = pd.read_csv('C:/Prediction with Python/Predictive Modelling/Customer Churn Model.csv')
print(data_columns.columns)
data_column_list = data_columns['Area Code'].tolist()
#data.columns.values
#data=pd.read_csv('C:/Prediction with Python/Predictive Modelling/Customer Churn Model.txt',header=None,names=data_column_list)
#data
Index(['State,Account Length,Area Code,Phone,Int'l Plan,VMail Plan,VMail Message,Day Mins,Day Calls,Day Charge,Eve Mins,Eve Calls,Eve Charge,Night Mins,Night Calls,Night Charge,Intl Mins,Intl Calls,Intl Charge,CustServ Calls,Churn?'], dtype='object')
--------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             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: 'Area Code'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-46-2b1c87f32751> in <module>
      5 data_columns = pd.read_csv('C:/Prediction with Python/Predictive Modelling/Customer Churn Model.csv')
      6 print(data_columns.columns)
----> 7 data_column_list = data_columns['Area Code'].tolist()
      8 #data.columns.values
      9 #data=pd.read_csv('C:/Prediction with Python/Predictive Modelling/Customer Churn Model.txt',header=None,names=data_column_list)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items))[isna(self.items)]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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: 'Area Code'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding the median of a column in a huge CSV file markagregory 5 1,733 Jan-24-2023, 04:22 PM
Last Post: DeaD_EyE
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,245 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  How to print a column name in csv file Truman 1 4,368 Mar-31-2020, 03:34 AM
Last Post: Larz60+
  pandas change row value an existing column with conditionals Gigux 1 2,921 Jun-22-2019, 08:04 PM
Last Post: Gigux
  copy one column from csv file and paste into xls file kprogrammer 0 4,347 Nov-03-2018, 04:03 PM
Last Post: kprogrammer
  Output column names Scott 2 2,683 Jun-25-2018, 12:47 PM
Last Post: volcano63
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 4,414 Mar-19-2018, 05:34 PM
Last Post: bentaz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020