Skip to content

Pandasの重複削除します。

架空データの作成

python
import datetime
import pandas as pd

# 架空のリスト作成
def make_probe(_list, s, e):
    for _id in range(s, e):
        # 日付
        dt = datetime.datetime(2018, 2, 1, 9, 15, 30)
        _list.append([_id+1, d)
    return _list

# リスト作成
probe_list = 
probe_list = make_probe(probe_list, 0, 4)
probe_list = make_probe(probe_list, 0, 10)

# データフレーム化
df = pd.DataFrame(probe_list, columns=['id', 'datetime'])
df.head()
iddatetime
12018-02-01 09:15:30
22018-02-01 09:15:30
32018-02-01 09:15:30
42018-02-01 09:15:30
12018-02-01 09:15:30

Pandasの重複削除

python
# 重複削除
df_drop = df['id'].drop_duplicates()
display(df_drop.head())

# 重複削除
df_drop = df[['id', 'datetime']].drop_duplicates()
display(df_drop.head())
indexid
01
12
23
34
85
indexiddate
012018-02-01 09:15:30
122018-02-01 09:15:30
232018-02-01 09:15:30
342018-02-01 09:15:30
852018-02-01 09:15:30

まとめ

Pandasの重複削除しました.

参考サイト