In [2]:
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
In [3]:
# 重複したデータを含むDataFrame
dframe = DataFrame({'key1': ['A'] * 2 + ['B'] * 3,
                  'key2': [2, 2, 2, 3, 3]})
dframe
Out[3]:
key1 key2
0 A 2
1 A 2
2 B 2
3 B 3
4 B 3
In [4]:
# 重複したデータがあるかどうかがわかります。
dframe.duplicated()
Out[4]:
0    False
1     True
2    False
3    False
4     True
dtype: bool
In [5]:
# 重複した行を削除できます。
dframe.drop_duplicates()
Out[5]:
key1 key2
0 A 2
2 B 2
3 B 3
In [6]:
# 1つの列に注目して、重複を削除できます。
dframe.drop_duplicates(['key1'])
Out[6]:
key1 key2
0 A 2
2 B 2
In [7]:
# 元のデータです。
dframe
Out[7]:
key1 key2
0 A 2
1 A 2
2 B 2
3 B 3
4 B 3
In [8]:
# 最初の重複ではなく、最後のデータを残す事もできます。
dframe.drop_duplicates(['key1'],take_last=True)
Out[8]:
key1 key2
1 A 2
4 B 3