10-lesson from Dataframe to Excel from Excel to Dataframe from Dataframe to JSON, from JSON to Dataframe
Import pandas as PD
import sys
Print (' Python version ' + sys.version)
print (' Pandas version ' + pd.__version__)
Python version 3.6.1 | Packaged by Conda-forge | (Default, Mar 2017, 21:57:00)
[GCC 4.2.1 compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Pandas version 0.19.2
from Dataframe to Excel
# Create a dataframe
d = [1,2,3,4,5,6,7,8,9]
df = PD. Dataframe (d, columns = [' number '])
DF
|
Number |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
5 |
6 |
6 |
7 |
7 |
8 |
8 |
9 |
# Export to Excel
df.to_excel ('./lesson10.xlsx ', Sheet_name = ' testing ', index = False)
print (' Done ')
Done
# from Excel to Dataframe
# Path to excel file
# Modify file path as you requested
location = R './lesson10.xlsx '
# Read in Excel file
df = pd.read_excel (location , 0)
Df.head ()
|
Number |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
Df.dtypes
Number Int64
dtype:object
Df.tail ()
|
Number |
4 |
5 |
5 |
6 |
6 |
7 |
7 |
8 |
8 |
9 |
from Dataframe to JSON
Df.to_json (' Lesson10.json ')
print (' Done ')
Done
from JSON to Dataframe
# Modify the file path according to your requirements
Jsonloc = R './lesson10.json '
# Read json file
df2 = Pd.read_json (Jsonloc)
Df2
|
Number |
0 |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
5 |
6 |
6 |
7 |
7 |
8 |
8 |
9 |
Df2.dtypes
Number Int64
dtype:object