-2

I have an Excel file which has numerical data of a matrix of dimension 30 x 30. I am trying to read it and access individual element as normally done. But I am getting a list instead of a single element.

Code

import pandas as pd
import numpy as np

xl  = pd.ExcelFile('sample.xlsx')
df1 = xl.parse('Sheet1')

data = np.matrix(df1)
print(data.shape)

print(data[0])
print(data[0][0])

Output

(30, 30)
[[ 0  0  7  0  4  0  3  0  0  7  4  0  0  0  3  2  0  0  0  0  4  0  0  0
   0  0  4  0 10  3]]
[[ 0  0  7  0  4  0  3  0  0  7  4  0  0  0  3  2  0  0  0  0  4  0  0  0
   0  0  4  0 10  3]]

Any thoughts

Chillie
  • 1,356
  • 13
  • 16
Atinesh
  • 1,790
  • 9
  • 36
  • 57
  • https://stackoverflow.com/questions/22169325/read-excel-file-in-python ... please go through the link – ak3191 Sep 17 '18 at 15:12

2 Answers2

0

You can read excel using xlrd module.Below code you can try to read the first column of the Excel.

import xlrd

loc = ("Excel_path")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0) 
row_count = sheet.nrows
while (n<row_count):
    data=sheet.cell_value(n, 0)#to read data from first column of excel

In data=sheet.cell_value(n, 0) you can replace 0 with 1 to read next column and so on.

mohit sehrawat
  • 171
  • 1
  • 12
-1

Try the below code

import pandas as pd 
import xlrd as xl 
from pandas import ExcelWriter
from pandas import ExcelFile 

df=pd.read_excel("sample.xlsx",sheet_name='Sheet1')

Apart from above, there are a bunch of answers for this question. Please go through them:-

Read Excel File in Python

ak3191
  • 583
  • 5
  • 14