pands DataFrame集合操作マニュアル
2702 ワード
イメージ.png
pandsのData Frameの集合操作についてはいつもめまいがします。主にscalaやjavaではこのような操作はあまりないです。一番苦手なのは二次元配列の操作です。
まずDataframeを初期化します。
pandsのData Frameの集合操作についてはいつもめまいがします。主にscalaやjavaではこのような操作はあまりないです。一番苦手なのは二次元配列の操作です。
まずDataframeを初期化します。
%python
import pandas as pd
import numpy as np
df2 = pd.DataFrame({ 'A' : [1,2,3,4],'B' : pd.Timestamp('20130102'),'C' : pd.Series(1,index=list(range(4)),dtype='float32'),'D' : np.array([3] * 4,dtype='int32'), 'E' : pd.Categorical(["test","train","test","train"]),'F' : 'foo' })
生成されたData Frameを表示します。%python
df2
出力を得るout--
A B C D E F
0 1 2013-01-02 1.0 3 test foo
1 2 2013-01-02 1.0 3 train foo
2 3 2013-01-02 1.0 3 test foo
3 4 2013-01-02 1.0 3 train foo
次は単一の列を取得します。%python
df2['E']
結果は--out---
0 test
1 train
2 test
3 train
Name: E, dtype: category
Categories (2, object): [test, train]
33次は、対応する列が本当にインデックス行のブール値かどうかを判断するものです。%python
df2['E']=="train"
結果は---out---
0 False
1 True
2 False
3 True
Name: E, dtype: bool
333ブール値は本当のインデックス出力対応行です。%python
df2[df2['E']=="train"]
結果は---out---
A B C D E F
1 2 2013-01-02 1.0 3 train foo
3 4 2013-01-02 1.0 3 train foo
333ブール値は本当のインデックスラインに対応する列の値を出力します。%python
df2[df2['E']=="train"]["D"]
結果は--out--
1 3
3 3
Name: D, dtype: int32
複数のDataframeを組み合わせて操作してもいいです。df 3とdf 2は少し違っています。%python
df3 = pd.DataFrame({ 'A' : [1,2,3,4],'B' : pd.Timestamp('20130102'),'C' : pd.Series(1,index=list(range(4)),dtype='float32'),'D' : np.array([3] * 4,dtype='int32'), 'E' : pd.Categorical(["test","train","fa","train"]),'F' : 'zoo' })
df3
--out---
A B C D E F
0 1 2013-01-02 1.0 3 test zoo
1 2 2013-01-02 1.0 3 train zoo
2 3 2013-01-02 1.0 3 fa zoo
3 4 2013-01-02 1.0 3 train zoo
```
DF3 DF2
```
%python
df2["C"][df3["E"]=="fa"]=3.0
df2
```
---out---A B C D E 0 1 2013-01-02 1.0 3 test foo 1 2 2013-01-02 1.0 3 trin foo 2 3 2013-01-02 3.0 3 test foo 3 2013-01-02 1.0 3 trin foo C 1.0 3.0,
[df3["E"]=="fa"] df2
pandas ,