Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Getting Error: "ValueError: 2 is not in range" when working with Pandas

$
0
0

I have got this error when try split my one column to few columns. But it split on just on one or two columns.If you wanna split on 3,4,5 columns it writes:

ValueError                                Traceback (most recent call last)/usr/local/Cellar/jupyterlab/2.1.5/libexec/lib/python3.8/site-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)    349             try:--> 350                 return self._range.index(new_key)    351             except ValueError:ValueError: 2 is not in rangeDuring handling of the above exception, another exception occurred:KeyError                                  Traceback (most recent call last)<ipython-input-19-d4e6a4d03e69> in <module>     22 data_old[Col_1_Label] = newz[0]     23 data_old[Col_2_Label] = newz[1]---> 24 data_old[Col_3_Label] = newz[2]     25 #data_old[Col_4_Label] = newz[3]     26 #data_old[Col_5_Label] = newz[4]/usr/local/Cellar/jupyterlab/2.1.5/libexec/lib/python3.8/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]/usr/local/Cellar/jupyterlab/2.1.5/libexec/lib/python3.8/site-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)    350                 return self._range.index(new_key)    351             except ValueError:--> 352                 raise KeyError(key)    353         return super().get_loc(key, method=method, tolerance=tolerance)    354 KeyError: 2

There is my code.I have csv file.And when pandas read it - create one column with value 'Контракт'.Then. I split it on another columns. But it split till two columns.I wanna 7 columns!Help please to understand this logic!

import pandas as pdfrom pandas import Series, DataFrameimport redframe1 = pd.read_csv('po.csv')columns = ['Контракт']data_old = pd.read_csv('po.csv', header=None, names=columns)data_old# The thing you want to split the column onSplitOn = ':'# Name of Column you want to splitSplit_Col = 'Контракт'newz = data_old[Split_Col].str.split(pat=SplitOn, n=-1, expand=True)# Column Labels (you can add more if you will have more)Col_1_Label = 'Номертелефону'Col_2_Label = 'Тарифнийпакет'Col_3_Label = 'ВихіднідзвінкизУкраїнизакордон'Col_4_Label = 'ВАРТІСТЬПАКЕТА/ЩОМІСЯЧНАПЛАТА'Col_5_Label = 'ЗАМОВЛЕНІДОДАТКОВІПОСЛУГИЗАМЕЖАМИПАКЕТА'Col_6_Label = 'Вартістьпослуги"Корпоративнамережа'Col_7_Label = 'ЗАГАЛОМЗАКОНТРАКТОМ (БЕЗПДВТАПФ)'data_old[Col_1_Label] = newz[0]data_old[Col_2_Label] = newz[1]data_old[Col_3_Label] = newz[2]#data_old[Col_4_Label] = newz[3]#data_old[Col_5_Label] = newz[4]#data_old[Col_6_Label] = newz[5]#data_old[Col_7_Label] = newz[6]data_old

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>