格式化
由於列名有空格,最好用逗號分隔。
算法
可以使用熊貓庫做到這一點:
import tempfile
import pandas
# create a temporary csv file with your data (comma delimited)
temp_file_name = None
with tempfile.NamedTemporaryFile('w', delete=False) as f:
f.write("""Date of event,Name,Date of birth
06.01.1986,John Smit,23.08.1996
18.12.1996,Barbara D,01.08.1965
12.12.2001,Barbara D,01.08.1965
17.10.1994,John Snow,20.07.1965""")
temp_file_name = f.name
# read the csv data using the pandas library, specify columns with dates
data_frame = pandas.read_csv(
temp_file_name,
parse_dates=[0,2],
dayfirst=True,
delimiter=','
)
# use groupby and max to do the magic
unique_rows = data_frame.groupby(['Name','Date of birth']).max()
# write the results
result_csv_file_name = None
with tempfile.NamedTemporaryFile('w', delete=False) as f:
result_csv_file_name = f.name
unique_rows.to_csv(f)
# read and show the results
with open(result_csv_file_name, 'r') as f:
print(f.read())
這導致:
Name,Date of birth,Date of event
Barbara D,1965-08-01,2001-12-12
John Smit,1996-08-23,1986-01-06
John Snow,1965-07-20,1994-10-17
'找到獨特rows'或'找到一個重複的row'? –
找到唯一的行,我也需要將這個解決方案與源列結合...並寫入csv –
與源結合意味着什麼?唯一的來源是源,如果與非唯一結合使用,結果是污染。 –