[Nuag] Today I Learned - #5 Reading Excel Files using Python Pandas

in #blog5 years ago (edited)


source

Hello Steempress and friends!

Today I bumped into a situation that I need to organize a long list of data. The first thing that came to my mind is open the Excel App and start work off from it.

However, the data set itself has been complied and what I want to retreive from the data set is simply the summary of it. I stop and started to think is there any way to do it faster?

The answer is always YES when it comes to Python! (MIGHTY PYTHON!)

What you want to do is, if you happened to have python installed on your computer, open a text editor and input the following into the python script:

import pandas as pd
df = pd.read_excel('Excel_Sample.xlsx',sheetname='Sheet1')
df[0]

By importing the pandas library, you can then use the read_excel function to read excel files using python. Here we have an excel file called 'Excel_Sample.xlsx' which has a sheet named 'Sheet1'.

However, when you run the script, you might encounter this:

ImportError: No module named 'xlrd'

To fix this, you will have to install another library called 'xlrd'.
you can do this by installing thru terminal:

pip install xlrd

or if you have conda installed,

conda install xlrd

Re-run the script again then you will see your excel file being read.

Having the data set being read, you can then leverage the powerful pandas library to do as much work you could have done with Excel but in a much faster pace!


今天我碰到了一個需要組織一長串數據的情況。我想到的第一件事就是打開Excel App並開始工作。

但是,數據集本身已經被編譯,我想從數據集中檢索的只是它的摘要。我停下來想有沒有辦法可以更快地做到這一點?

對於Python來說,這種工作真是易過借火 (MIGHTY PYTHON!)

如果你碰巧在你的計算機上安裝了python,你只需要,打開一個文本編輯器並將以下內容輸入到python腳本中:

import pandas as pd
df = pd.read_excel('Excel_Sample.xlsx',sheetname ='Sheet1')
df[0]

通過導入pandas庫,您可以使用read_excel函數使用python讀取excel文件。這裡我們有一個名為'Excel_Sample.xlsx'的excel文件,它有一個名為'Sheet1'的工作表。

但是,當您運行腳本時,您可能會遇到以下情況:

ImportError: No module named 'xlrd'

要解決此問題,您必須安裝另一個名為“xlrd”的庫。
你可以通過終端來安裝:

pip install xlrd

或者如果你安裝了conda,

conda安裝xlrd

再次重新運行腳本,然後您將看到正在讀取您的Excel文件。

讀取數據集之後,您可以利用功能強大的pandas庫來完成Excel所能完成的工作,但速度要快得多!

如果您有任何疑問,可以留言大家討論討論。


Posted from my blog with SteemPress : http://nuagnorab.vornix.blog/nuag-til-5-reading-excel-files-using-python-pandas/


Sponsored ( Powered by dclick )
Introducing DCLICK: An Incentivized Ad platform by Proof of Click. - Steem based AdSense.

Hello, Steemians. Let us introduce you a new Steem B...

logo

This posting was written via
dclick the Ads platform based on Steem Blockchain.

Sort:  

你今天过的开心吗?这是哪里?你是谁?我为什么会来这边?你不要给我点赞不要点赞,哈哈哈哈哈哈。倘若你想让我隐形,请回复“取消”。

Coin Marketplace

STEEM 0.32
TRX 0.12
JST 0.033
BTC 64647.16
ETH 3160.49
USDT 1.00
SBD 4.13