- Python:Advanced Predictive Analytics
- Ashish Kumar Joseph Babcock
- 275字
- 2021-07-02 20:09:21
Case 4 – miscellaneous cases
Apart from the standard cases described previously, there are certain less frequent cases of data file handling that might need to be taken care of. Let's have a look at two of them.
Reading from an .xls or .xlsx file
Go to the Google Drive and look for .xls
and .xlsx
versions of the Titanic dataset. They will be named titanic3.xls
and titanic3.xlsx
. Download both of them and save them on your computer. The ability to read Excel files with all its sheets is a very powerful technique available in pandas. It is done using a read_excel
method, as shown in the following code:
import pandas as pd data=pd.read_excel('E:/Personal/Learning/Predictive Modeling Book/Book Datasets/titanic3.xls','titanic3') import pandas as pd data=pd.read_excel('E:/Personal/Learning/Predictive Modeling Book/Book Datasets/titanic3.xlsx','titanic3')
It works with both, .xls
and .xlsx
files. The second argument of the read_excel
method is the sheet name that you want to read in.
Another available method to read a delimited data is read_table
. The read_table
is exactly similar to read_csv
with certain default arguments for its definition. In some sense, read_table
is a more generic form of read_csv
.
Writing to a CSV or Excel file
A data frame can be written in a CSV or an Excel file using a to_csv
or to_excel
method in pandas. Let's go back to the df
data frame that we created in Case 2 – reading a dataset using the open method of Python. This data frame can be exported to a directory in a CSV file, as shown in the following code:
df.to_csv('E:/Personal/Learning/Predictive Modeling Book/Book Datasets/Customer Churn Model.csv'
Or to an Excel file, as follows:
df.to_excel('E:/Personal/Learning/Predictive Modeling Book/Book Datasets/Customer Churn Model.csv'
- Python廣告數(shù)據(jù)挖掘與分析實(shí)戰(zhàn)
- Spark核心技術(shù)與高級(jí)應(yīng)用
- OracleDBA實(shí)戰(zhàn)攻略:運(yùn)維管理、診斷優(yōu)化、高可用與最佳實(shí)踐
- 白話(huà)大數(shù)據(jù)與機(jī)器學(xué)習(xí)
- 金融商業(yè)算法建模:基于Python和SAS
- Python數(shù)據(jù)分析與數(shù)據(jù)化運(yùn)營(yíng)
- Splunk智能運(yùn)維實(shí)戰(zhàn)
- 聯(lián)動(dòng)Oracle:設(shè)計(jì)思想、架構(gòu)實(shí)現(xiàn)與AWR報(bào)告
- 中文版Access 2007實(shí)例與操作
- 實(shí)現(xiàn)領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)
- 大數(shù)據(jù)時(shí)代系列(套裝9冊(cè))
- Oracle 11g數(shù)據(jù)庫(kù)管理員指南
- Oracle 內(nèi)核技術(shù)揭密
- 數(shù)字化轉(zhuǎn)型方法論:落地路徑與數(shù)據(jù)中臺(tái)
- AI Crash Course