官术网_书友最值得收藏!

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'
主站蜘蛛池模板: 民权县| 宜川县| 桂林市| 尼玛县| 延川县| 精河县| 开远市| 贞丰县| 涞水县| 佛坪县| 开封市| 上虞市| 昌图县| 拉萨市| 武汉市| 讷河市| 米脂县| 玉环县| 汝州市| 美姑县| 洞头县| 永善县| 长岛县| 新巴尔虎右旗| 和龙市| 正定县| 罗平县| 溧阳市| 衡东县| 西青区| 宁安市| 乐至县| 红桥区| 桑日县| 武功县| 齐齐哈尔市| 德格县| 楚雄市| 衢州市| 鸡东县| 两当县|