1. 程式人生 > >DataFrame基本操作

DataFrame基本操作

randn range 在一起 light 百度 column dataframe data 網上

這些操作在網上都可以百度得到,為了便於記憶自己再根據理解總結在一起。---------勵誌做一個優雅的網上搬運工

1.建立dataframe

(1)Dict to Dataframe

df = pd.DataFrame({‘key1‘:[‘a‘,‘a‘,‘b‘,‘b‘,‘a‘],‘key2‘:[‘one‘,‘two‘,‘one‘,‘two‘,‘one‘],‘data1‘:np.random.randn(5),‘data2‘:np.random.randn(5)})
df
      data1     data2 key1 key2
0 -0.484486 -1.404184    a  one
1 -1.541437  0.549591    a  two
2 -0.015287 -1.589111    b  one
3 -0.069614 -0.513824    b  two
4 -0.704788  0.395147    a  one

(2)Series to Dataframe

df2=pd.DataFrame(np.arange(16).reshape((4,4)),index=[‘one‘,‘two‘,‘three‘,‘four‘],columns=[‘a‘,‘b‘,‘c‘,‘d‘])
df2
        a   b   c   d
one     0   1   2   3
two     4   5   6   7
three   8   9  10  11
four   12  13  14  15

(3)pd.read_csv()

2.groupby

DataFrame基本操作