Lesson 10

  • From DataFrame to Excel
  • From Excel to DataFrame
  • From DataFrame to JSON
  • From JSON to DataFrame
In [14]:
from pandas import DataFrame, read_excel
import pandas as pd
import sys
In [15]:
print 'Python version ' + sys.version
print 'Pandas version: ' + pd.__version__
Python version 2.7.5 |Anaconda 1.8.0 (64-bit)| (default, Jul  1 2013, 12:37:52) [MSC v.1500 64 bit (AMD64)]
Pandas version: 0.14.0

From DataFrame to Excel

In [16]:
# Create DataFrame
d = [1,2,3,4,5,6,7,8,9]
df = DataFrame(d, columns = ['Number'])
df
Out[16]:
Number
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
In [17]:
# Export to Excel
df.to_excel('Lesson10.xlsx', sheet_name = 'testing', index = False)
print 'Done'
Done

From Excel to DataFrame

In [18]:
# Path to excel file
# Your path will be different, please modify the path below.
location = r'C:\Users\david\notebooks\pandas\Lesson10.xlsx'

# Parse the excel file
df = read_excel(location, 0)
df.head()
Out[18]:
Number
0 1
1 2
2 3
3 4
4 5
In [19]:
df.dtypes
Out[19]:
Number    int64
dtype: object
In [20]:
df.tail()
Out[20]:
Number
4 5
5 6
6 7
7 8
8 9

From DataFrame to JSON

In [21]:
df.to_json('Lesson10.json')
print 'Done'
Done

From JSON to DataFrame

In [22]:
# Your path will be different, please modify the path below.
jsonloc = r'C:\Users\david\notebooks\pandas\Lesson10.json'

# read json file
df2 = pd.read_json(jsonloc)
In [23]:
df2
Out[23]:
Number
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
In [24]:
df2.dtypes
Out[24]:
Number    int64
dtype: object

Author: David Rojas LLC