3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

個人的に忘れがちなpandasの書き方備忘録

Last updated at Posted at 2020-06-22

作業用にSampleのDataFrameを作成

df = pd.util.testing.makeMixedDataFrame()
df
A B C D
0 0.0 0.0 foo1 2009-01-01
1 1.0 1.0 foo2 2009-01-02
2 2.0 0.0 foo3 2009-01-05
3 3.0 1.0 foo4 2009-01-06
4 4.0 0.0 foo5 2009-01-07

pandasの表示オプションの変更

pd.set_option('max_rows', 2)
pd.set_option('max_columns', 3)
df
A ... D
0 0.0 ... 2009-01-01
... ... ... ...
4 4.0 ... 2009-01-07

複数のカラムの型を変更

df = df.astype({"A":"int64", "B":"int64"})
df.dtypes

A int64
B int64
C object
D datetime64[ns]
dtype: object

複数のカラムのフォーマットを変更

df.assign(E=10000*df["A"])\
    .assign(F=100*df["B"])\
    .style.format(
    {
        "A":"{:.2f}", 
        "B":"{:.4f}", 
        "D":"{:%Y-%m-%d}",
        "E":"{:,}",
        "F":"{:}%"
    }
)
A B C D E F
0 0.00 0.0000 foo1 2009-01-01 0 0%
1 1.00 1.0000 foo2 2009-01-02 10,000 100%
2 2.00 0.0000 foo3 2009-01-05 20,000 0%
3 3.00 1.0000 foo4 2009-01-06 30,000 100%
4 4.00 0.0000 foo5 2009-01-07 40,000 0%

groupbyする時のkeyをindexにしない

df.groupby("B", as_index=False).agg({"A":"sum", "A":"mean"})
B A
0 0 2
1 1 2

Multi Indexの解除

df.columns = ["_".join(index) for index in df.columns]
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?