Pandas的DataFrame資料型別

2021-09-26 06:12:25 字數 1540 閱讀 6561

pandas的dataframe資料型別

縱軸表示不同索引axis=0,橫軸表示不同列axis=1

dataframe型別建立

1.從二維ndarray物件建立

import pandas as pd

import numpy as np

d=pd.dataframe(np.arange(10)

.reshape(2,

5))d

out[4]

:012

3400

1234

1567

89#自動生成行索引和列索引

2.從一維ndarray物件字典建立

import pandas as pd

dt=dt

out[9]

:d=pd.dataframe(dt)

#原字典中的鍵變成列索引值,列索引位值中的series資料中的索引並集

dout[11]

: one two

a 1.0

8b 2.0

7c 3.0

6w nan 5

pd.dataframe(dt,index=

['a'

,'b'

,'c'

],columns=

['two'

,'three'])

out[13]

: two three

a 8 nan

b 7 nan

c 6 nan #缺少的元素會被自動補齊

3.從列表型別的字典建立

import pandas as pd

dl=pd.dataframe(dl,index=

['a'

,'b'

,'c'

,'d'])

#這裡注意後加的索引值得跟字典裡的值數一樣

pandas中dict和dataFrame互轉

pd.dataframe dict a 使用df.to dict 缺省會把key和值分開 引數 dict 預設 list series split records index 如果是list dict 這種巢狀情況轉的df,迴轉需要使用records 拿上面的資料舉例,df b a b c 0 0 ...

pandas的資料結構之DataFrame

dataframe是乙個 型的資料結構,它含有一組有序的列,每列可以是不同資料型別的資料。dataframe既有行索引也有列索引,可以將它看作為乙個由series組成的字典 共用同乙個索引 dataframe中的資料是以乙個或多個二維塊儲存的,而不是列表 字典或別的一維資料結構。a 通過字典建立,字...

pandas中的資料結構 DataFrame

型的資料結構 修改某一行 frame.values 0 d 2 frame name1 pay2 x d 2 y b 6000 z c 9000 修改某一行的值 frame.values 1 1 9000 frame name1 pay2 x d 2 y b 9000 z c 9000 獲取某行資料...