I keep getting this error when trying to add an empty column in an imported CSV.
"IndexError: index 27 is out of bounds for axis 0 with size 25"
The original CSV spans A-Z (0-25 columns) then AA, AB, AC, AD (26, 27, 28, 29).
The csv with the error currently stretches A-Z but the error occurs when trying to add the column after then - in this case AA. I guess that would be 26.
Here is the code:
```
#import CSV to dataframe
orders = pd.read_csv("Orders.csv", header=None)
#copy columns needed from order to ordersNewCols
ordersNewCols = orders.iloc[:,[1, 3, 11, 12, 15]]
#create new dataframe - ordersToSubmit
ordersToSubmit = pd.DataFrame()
#copy columns from ordersNewCols to ordersToSubmit
ordersToSubmit = ordersNewCols.copy()
ordersToSubmit.to_csv("ordersToSubmit.csv", index=False)
#Insert empty columns where needed.
ordersToSubmit.insert(2,None,'')
ordersToSubmit.insert(3,None,'')
ordersToSubmit.insert(4,None,'')
ordersToSubmit.insert(6,None'')
ordersToSubmit.insert(7,None,'')
ordersToSubmit.insert(8,None'')
ordersToSubmit.insert(9,None,'')
ordersToSubmit.insert(10,None,'')
ordersToSubmit.insert(11,None,'')
ordersToSubmit.insert(12,None,'')
ordersToSubmit.insert(13,None,'')
ordersToSubmit.insert(14,None,'')
ordersToSubmit.insert(15,None,'')
ordersToSubmit.insert(16,None,'')
ordersToSubmit.insert(18,None,'')
ordersToSubmit.insert(19,None,'')
ordersToSubmit.insert(20,None,'')
ordersToSubmit.insert(21,None,'')
ordersToSubmit.insert(22,None,'')
ordersToSubmit.insert(23,None,'')
ordersToSubmit.insert(27,None,'')
IndexError: index 27 is out of bounds for axis 0 with size 25
'''
How do I expand it to not bring up the error?
CSV screenprint
CodePudding user response:
Without having a look at your csv file, it is hard to tell what is causing this issue.
Anyways...
From pandas.DataFrame.insert documentation
locint
Insertion index. Must verify 0 <= loc <= len(columns).
As you can see, it says loc must be between len(columns)
, so what you are trying is illegal according to this. I think if you try to insert on an index that is less than len(colomns)
, it will shift remaining column by 1 to the right
CodePudding user response:
Practically your indexing starts from 0, thus the column index of the 25th element is 24 (not 25). To expand your dataframe with size 25, you need to change the line below:
ordersToSubmit.insert(27,None,'') -> ordersToSubmit.insert(25,None,'')
25 is the index of the 26th element, thus expanding your dataframe by one column. Indexes 25 and above are out of bounds in this case.