0

I am trying to import data from an excel file. I would like to import from a specific sheet and in that specific sheet only import certain columns. The code I am using is as follows:

%matplotlib inline

import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="darkgrid")

# Input Portfolio data

xl_file=pd.ExcelFile('C:/Users/e877780/Desktop/DEV/S2_SK_S_06_02_input.xlsx')
s2_0602=xl_file.parse("S.06.02")[('C0080','C0090')]

My idea is to only import columns C0080 and C0090. I I just use one, it works but also does not keep the column name. Can someone help Thks

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Use [pandas read_excel](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html). You can specify the sheetname and columns to use. – m13op22 Mar 28 '19 at 15:45
  • 1
    Specifically [this answer](https://stackoverflow.com/a/36590692/3250829) from the duplicate. – rayryeng Mar 28 '19 at 15:47

1 Answers1

0

You can try this:

xl_file=pd.ExcelFile('C:/Users/e877780/Desktop/DEV/S2_SK_S_06_02_input.xlsx')
sheet1=xl_file.parse("S.06.02")
s2_0602 = sheet1[['C0080','C0090']]

Hope it help

miuxu
  • 181
  • 1
  • 4