[PYTHON] How to use Pandas 2

1 This article

Make a note of various operation methods for DataFrame type data.

2 Contents

2-1 Multi-Index type DataFrame data Display data of only a specific index.

sample.py


import pandas as pd
import numpy as np
idx = pd.IndexSlice

#Define data.
dat = [
    [100,'2019-07-01','9997','740'],
    [100,'2019-07-02','9997','749'],
    [100,'2019-07-03','9997','757'],
    [200,'2019-07-01','9997','769'],
    [200,'2019-07-02','9997','762'],
    [200,'2019-07-03','9997','760']
]

#Store dat in DataFrame type variable df.
df = pd.DataFrame(dat,columns=["A","B","C","D"])

#A,Specify column B as index.
df=df.set_index(["A","B"])

print("Show df","\n",df)

#index=Extract 100 data.
df=df.loc[idx[100, :], :]

print("df to index=Extract 100 data.","\n",df)


#index=100,Extract the data in column D.
df=df.loc[idx[100, :], ["D"]]

print("df to index=100,Extract the data in column D.","\n",df)


Execution result

Show df
                    C    D
A   B                    
100 2019-07-01  9997  740
    2019-07-02  9997  749
    2019-07-03  9997  757
200 2019-07-01  9997  769
    2019-07-02  9997  762
    2019-07-03  9997  760
df to index=Extract 100 data.
                    C    D
A   B                    
100 2019-07-01  9997  740
    2019-07-02  9997  749
    2019-07-03  9997  757


df to index=100,Extract the data in column D.
                   D
A   B              
100 2019-07-01  740
    2019-07-02  749
    2019-07-03  757

Recommended Posts

How to use Pandas 2
How to use Pandas Rolling
[Python] How to use Pandas Series
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[Python] Summary of how to use pandas
[Pandas] What is set_option [How to use]
How to use pandas Timestamp and date_range
How to use Qt Designer
How to use search sorted
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)