Make a note of various operation methods for DataFrame type data.
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