5 28學習筆記(pandas)

2021-09-23 20:43:51 字數 4923 閱讀 5570

取數

通過位置選擇

import pandas as pd

import numpy as np

dates = pd.date_range('20190101',periods=6)

df=pd.dataframe(np.random.randn(6,4),index=dates,columns=list('abcd'))

print(df)

print(df.iloc[3]) //讀取第三行

結果:

a b c d

2019-01-01 -0.612854 -0.253183 -1.373708 -2.301114

2019-01-02 0.212331 -0.609568 -0.392484 0.825583

2019-01-03 1.840755 0.027806 1.224755 -0.624368

2019-01-04 1.036859 -1.144750 1.551773 0.457958

2019-01-05 1.965845 -0.997880 -0.845364 0.012153

2019-01-06 -1.605254 1.835839 0.280078 1.448922

a 1.036859

b -1.144750

c 1.551773

d 0.457958

name: 2019-01-04 00:00:00, dtype: float64

整數切片

print(df.iloc[3:5,0:2])
結果:

a b2019-01-04 1.036859 -1.14475

2019-01-05 1.965845 -0.99788

print(df.iloc[[1,2,4],[0,2]])
結果:

a c2019-01-02 0.212331 -0.392484

2019-01-03 1.840755 1.224755

2019-01-05 1.965845 -0.845364

行的切片

print(df.iloc[1:3,:])
結果:

a b c d

2019-01-02 0.212331 -0.609568 -0.392484 0.825583

2019-01-03 1.840755 0.027806 1.224755 -0.624368

列的切片

print(df.iloc[:,1:3])
結果:

b c2019-01-01 -0.253183 -1.373708

2019-01-02 -0.609568 -0.392484

2019-01-03 0.027806 1.224755

2019-01-04 -1.144750 1.551773

2019-01-05 -0.997880 -0.845364

2019-01-06 1.835839 0.280078

獲取明確值

print(df.iloc[1,1])

print(df.iat[1,1]) \\結果一樣

結果:

-0.6095676716857732

根據單列的值來選擇資料

print(df[df.a>0])
結果:

a b c d

2019-01-02 0.212331 -0.609568 -0.392484 0.825583

2019-01-03 1.840755 0.027806 1.224755 -0.624368

2019-01-04 1.036859 -1.144750 1.551773 0.457958

2019-01-05 1.965845 -0.997880 -0.845364 0.012153

pycharm炸了,重啟

dfa b c d

2019-01-01 0.913584 -0.147238 -0.146727 0.642962

2019-01-02 -0.193907 1.500496 0.536562 0.536783

2019-01-03 0.773896 -1.628390 -0.299604 0.682026

2019-01-04 -0.107013 1.124117 1.049309 0.296538

2019-01-05 -1.194853 1.492495 -0.147292 -0.313903

2019-01-06 -0.658363 -0.209820 -0.126511 -0.902405

從滿足條件的dataframe中選擇值

print(df[df>0])
結果:

a b c d

2019-01-01 0.913584 nan nan 0.642962

2019-01-02 nan 1.500496 0.536562 0.536783

2019-01-03 0.773896 nan nan 0.682026

2019-01-04 nan 1.124117 1.049309 0.296538

2019-01-05 nan 1.492495 nan nan

2019-01-06 nan nan nan nan

isin()方法進行過濾

df2=df.copy()

df2['e']=['one','one','two','three','four','three']

print(df2)

print(df2['e'].isin(['two','four']))

結果:

a b c d e

2019-01-01 0.913584 -0.147238 -0.146727 0.642962 one

2019-01-02 -0.193907 1.500496 0.536562 0.536783 one

2019-01-03 0.773896 -1.628390 -0.299604 0.682026 two

2019-01-04 -0.107013 1.124117 1.049309 0.296538 three

2019-01-05 -1.194853 1.492495 -0.147292 -0.313903 four

2019-01-06 -0.658363 -0.209820 -0.126511 -0.902405 three

2019-01-01 false

2019-01-02 false

2019-01-03 true

2019-01-04 false

2019-01-05 true

2019-01-06 false

freq: d, name: e, dtype: bool

向現有資料增加乙個新列

import pandas as pd

d=df=pd.dataframe(d)

print('adding a new column by passing as series:')

df['three']=pd.series([10,20,30],index=['a','b','c'])

print(df)

print('adding a new column using the existing columns in dataframe:')

df['four']=df['one']+df['three']

print(df)

結果:

adding a new column by passing as series:

one two three

a 1.0 1 10.0

b 2.0 2 20.0

c 3.0 3 30.0

d nan 4 nan

adding a new column using the existing columns in dataframe:

one two three four

a 1.0 1 10.0 11.0

b 2.0 2 20.0 22.0

c 3.0 3 30.0 33.0

d nan 4 nan nan

刪除列

import pandas as pd

d=df=pd.dataframe(d)

print('our dataframe is:')

print(df)

print('deleting the first column using del functions:')

del df['one']

print(df)

print('deleting another column using pop function:')

df.pop('two')

print(df)

結果:

our dataframe is:

one two three

a 1.0 1 10.0

b 2.0 2 20.0

c 3.0 3 30.0

d nan 4 nan

deleting the first column using del functions:

two three

a 1 10.0

b 2 20.0

c 3 30.0

d 4 nan

deleting another column using pop function:

three

a 10.0

b 20.0

c 30.0

d nan

附加行34:23

Flex 學習筆記 5 28

canvas 中的元件的座標,讓元件的位置超出 canvas 的區域範圍,在發布程式,會發現 canvas 自動增加了滾動條。每個容器都自帶了滾動條,預設狀態下滾動條採用自動適應的原則,只有容器的內容超出了容器的可視範圍才顯示滾動條,我們可以通過 horizontalpolicy 和 vertica...

pandas學習筆記

import numpy as np import pandas as pd obj2 pd.series 4,7,5,3 index d b a c obj2 out 99 d 4 b 7 a 5 c 3 dtype int64 a b pd.series a bout 102 a 1 b 2 c...

pandas學習筆記

1 建立物件,瀏覽資料 建立物件,瀏覽資料 import pandas as pd import numpy as np import matplotlib.pyplot as plt 建立series s pd.series 1,2,4,6,np.nan,9,10 index list abcde...