LoginSignup
2
4

More than 5 years have passed since last update.

pandasの便利関数

Last updated at Posted at 2019-04-04

kaggleを始めたのですが、データの前処理でどうすればいいか途方にくれることが多いです。
karnelを読んで見つけたDataFrameの前処理・可視化に便利そうな関数のメモです。

dfは読み込まれてる前提です。

カラムの型、カラムのNanデータ数を一覧表示

df.info()

カラムのデータ例、平均、分散、標準偏差などを一覧表示

df.describe(include='all')

Nanデータの多いカラム順にソート

df.isna().sum().sort_values(ascending=False)

グラフ表示

missing = df.isna().sum().sort_values(ascending=False)
sns.barplot(missing, missing.index)
plt.show()

入れ子の文字列をリストと辞書に変換

csvのフィールドに以下のようなデータが文字列として格納されている場合がよくあります。

[{'id': 53, 'name': 'Thriller'}, {'id': 18, 'name': 'Drama'}]

この文字列はPythonのリストの中に複数の辞書が入っています。


import ast

x = "[{'id': 53, 'name': 'Thriller'}, {'id': 18, 'name': 'Drama'}]"

converted_x = ast.literal_eval(x)

literal_evalで文字列をリストや辞書に変換できます。

2
4
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
2
4