What makes Pandas so popular is it's ability to convert various types of files into DataFrames. Whether you're working with an Excel Workbook, CSV/TSV File, or JSON, Pandas can convert it into a DataFrame. This tutorial will cover the following learning objectives:
pd.read_excel('file_name.xlsx')
pd.read_excel('file_name.xlsx', sheet_name='sheet_name')
all_sheets = pd.read_excel('file_name.xlsx', sheet_name=None)
all_sheets[key]
pd.concat(pd.read_excel('file_name.xlsx', sheet_name=None), ignore_index=True)
pd.read_csv('file_name.csv')
pd.read_csv(r"C:\Users\name\Documents\test.csv")
pd.read_csv("C:\\Users\\name\\Documents\\test.csv")
data = {
"Column1": {"0":"row1_value1", "1":"row2_value1"},
"Column2": {"0":"row1_value2", "1":"row2_value2"},
"Column3": {"0":"row1_value3", "1":"row2_value3"}
}
pd.read_json('file_name.json')
pd.read_json('file_name.json', orient='columns')
data = {
"index":["0", "1"],
"columns":["Column1", "Column2", "Column3"],
"data":["row1_value1", "row1_value2", "row1_value3"],
["row2_value1" "row2_value2", "row2_value3"]
]
}
Syntax:pd.read_json('file_name.json', orient='split')
data = {
"0":{"Column1":"row1_value1", "Column2":"row1_value2", "Column3":"row1_value3"},
"1":{"Column1":"row2_value1", "Column2":"row2_value2", "Column3":"row2_value3"},
"2":{"Column1":"row3_value1", "Column2:"row3_value2", "Column3":"row3_value3" },
}
Syntax:pd.read_json('file_name.json', orient='index')
data = {
{"Column1":"row1_value1", "Column2":"row1_value2", "Column3":"row1_value3"},
{"Column1":"row2_value1", "Column2":"row2_value2", "Column3":"row2_value3"},
{"Column1":"row3_value1", "Column2:"row3_value2", "Column3":"row3_value3"},
}
Syntax:pd.read_json('file_name.json', orient='records')
data = [
["row1_value1", "row1_value2", "row1_value3"],
["row2_value1", "row2_value2", "row2_value3"],
["row3_value1", "row3_value2", "row3_value3"],
]
Syntax:pd.read_json('file_name.json', orient='values')
starting_value:ending_value.
If you want to slice values in reverse, use the following notation: starting_value:ending_value:-1
dataframe.to_csv('file_name.csv')
dataframe.to_csv('file_name.csv', index=False)
dataframe.to_csv('file_name.csv', headers=False)
dataframe.to_json('file_name.json', orient='orientation')
Congratulations! You just completed the Working with Text Files Tutorial! To help test your knowledge, let's
practice Reading and Writing some Text Files into and from DataFrames.
**It's highly recommended that you
complete the exercise outlined in the previous tutorial before beginning this exercise.**
Have any issues with the above exercise? Post your question on Discord!