Rani
Rani

Reputation: 91

Extracting column position instead of column names using Panda?

I am trying to use python to extract certain column from an excel file,I need to automate the process instead of copy and pasting. Some of column names are repeated to show different kind of data, eg. Jan-Dec is repeated several times. I basically need columns:B,C, I, L and EW:FM but I don't know how to write this in pandas. I am very new to python, any help will be appreciated.

Code:

import pandas as pd 
import numpy as np 
data=pd.read_excel('path', sheet_name="1. Sheet_name")
print(data)
header=data.iloc[5]
data=data[5:]
data.columns=header
print(data)
data_keep=data[['Name','BU','Account #','Acct Name','Final 
                Name','JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC','FY 2020F']]
print(data_keep)
data_keep.to_excel('path',index=False)

But when use the above code it gives me every single column with the name "JAN" to "Dec". I only need a particular set of dates. Is there anyway I can code the position of the column, instead of the name of the column. Result:

Excel Result

Upvotes: 1

Views: 47

Answers (1)

Deepak
Deepak

Reputation: 707

Take a look at this screenshot, probably it can help you. You can specify the column index. Please be noted that it starts from 0.

enter image description here

Upvotes: 1

Related Questions