Learning Pandas (10)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.