Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Python Pandas, data processing
petercour
petercour

Posted on

     

Python Pandas, data processing

Thepandas module lets you parse data. For instance, you can have excel data that you want to read.

excel

You can load an excel file with the method read_excel(filename), where filename may include a path. It can read both xls and xlsx.

That data is stored in a data frame. The data frame is a data structure in pandas, which you can edit orplot.

#!/usr/bin/python3# coding: utf-8import  pandas  as pddf = pd.read_excel('example.xls')data1 = df.head(7)data2 = df.valuesprint("A \n{0}".format(data1))print("B \n{0}".format(data2))
Enter fullscreen modeExit fullscreen mode

What we are doing here is very simple. I'll describe the steps.

Load the excel data. This file has to be in the same directory, else a path must be specified.

df = pd.read_excel('example.xls')
Enter fullscreen modeExit fullscreen mode

Store data from the data frame into variables

data1 = df.head(7)data2 = df.values
Enter fullscreen modeExit fullscreen mode

Output those variables. Because it's not a single value we format it.

print("A \n{0}".format(data1))print("B \n{0}".format(data2))
Enter fullscreen modeExit fullscreen mode

Run it in a terminal (or IDE if you prefer)

python3 example.py
Enter fullscreen modeExit fullscreen mode

Outputs the data from the excel:

A        id    name  class       date  stature0  201901   Aaron      1 2019-01-01        11  201902  Arthur      1 2019-01-02        12  201903   Angus      1 2019-01-03        13  201904  Albert      2 2019-01-04        24  201905  Adrian      2 2019-01-05        25  201906    Adam      3 2019-01-06        16  201907  Andres      3 2019-01-07        1B [[201901 'Aaron' 1 Timestamp('2019-01-01 00:00:00') 1] [201902 'Arthur' 1 Timestamp('2019-01-02 00:00:00') 1] [201903 'Angus' 1 Timestamp('2019-01-03 00:00:00') 1] [201904 'Albert' 2 Timestamp('2019-01-04 00:00:00') 2] [201905 'Adrian' 2 Timestamp('2019-01-05 00:00:00') 2] [201906 'Adam' 3 Timestamp('2019-01-06 00:00:00') 1] [201907 'Andres' 3 Timestamp('2019-01-07 00:00:00') 1] [201908 'Alex' 3 Timestamp('2019-01-08 00:00:00') 1]]
Enter fullscreen modeExit fullscreen mode

Related links:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More frompetercour

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp