英文原文: 10 - Lesson
- 从 DataFrame 到 Excel
- 从 Excel 到 DataFrame
- 从 DataFrame 到 JSON
- 从 JSON 到 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 23 2017, 21:57:00)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Pandas version 0.19.2
从 DataFrame 到 Excel
d = [1,2,3,4,5,6,7,8,9]
df = pd.DataFrame(d, columns = ['Number'])
df
df.to_excel('./Lesson10.xlsx', sheet_name = 'testing', index = False)
print('Done')
Done
# 从 Excel 到 DataFrame
location = r'./Lesson10.xlsx'
df = pd.read_excel(location, 0)
df.head()
df.dtypes
Number int64
dtype: object
df.tail()
从 DataFrame 到 JSON
df.to_json('Lesson10.json')
print('Done')
Done
从 JSON 到 DataFrame
jsonloc = r'./Lesson10.json'
df2 = pd.read_json(jsonloc)
df2
df2.dtypes
Number int64
dtype: object