当前位置: 首页 > 知识库问答 >
问题:

查找包含最大数据的重复行

张唯
2023-03-14

我有一个这样的csv文件

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

我必须按“姓名”和“出生日期”(可能与其他列一起)查找唯一的行,但必须按最大日期查找。

因此,我必须获得如下csv文件:

Date of event       Name        Date of birth
06.01.1986          John Smit   23.08.1996
12.12.2001          Barbara D   01.08.1965
17.10.1994          John Snow   20.07.1965

怎么做?我没有任何想法。。

共有2个答案

扈俊健
2023-03-14
import pandas as pd

# read the csv in with pandas module

df = pd.read_csv('pathToCsv.csv', header=0, parse_dates=[0, 2])

# set the column names as more programming friendly  i.e. no whitespace

df.columns = ['dateOfEvent','name','DOB'] # and probably some other columns ..

# keep row only with max (Date of event) per group ( name, Date of Birth )

yourwish = =df.groupby(['Name','DOB'])['dateOfEvent'].max()
步嘉德
2023-03-14

因为列名有空格,所以最好用逗号分隔。

您可以使用pandas库执行以下操作:

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
 类似资料:
  • 问题很简单,我有两个数据帧: > 一个有90000套公寓和他们的经纬度 还有一个有3000个药房和他们的经纬度 我想为我所有的公寓创建一个新变量:“最近药房的距离” 为此,我尝试了两种花费大量时间的方法: 第一种方法:我创建了一个矩阵,我的公寓排成一行,我的药店排成一列,它们之间的距离在交叉点上,然后我只取矩阵的最小值,得到一个90000值的列向量 我只是用了一个双人床来搭配numpy: ps:我

  • 我使用查找列表中的最大值,但是下面的代码返回,尽管最大值是。

  • 问题内容: 我有一个包含许多行的表,每个行都有一个名为“ RecordData”的列,其中包含我要搜索的XML数据。 下面给出了此列中三行示例xml数据的值: 1: 2: 3: 我正在使用以下SQL尝试查找任何表行,这些表行的XML数据包含特定的搜索词(在本示例中为‘1’)。 正如您将看到的,这依赖于出现在第一个“ RecordField”元素文本中的搜索词,而不是在所有“ RecordField

  • 我有super class Person扩展到2个sup class(Employee和Student),在我输入员工信息(姓名、SSN、工资、Gendr)后,我想找到最大工资的员工并键入他的信息,但我不知道如何用对象来做!,如果你可以给我一个提示,我将非常感谢。

  • 我正在尝试解决这个算法问题: https://dunjudge.me/analysis/problems/469/ 为了方便起见,我总结了下面的问题陈述。 给定一个长度为 ( 多数元素定义为发生的元素 时限:1.5s 例如: 如果给定的数组是[1,2,1,2,3,2], 答案是5,因为从位置1到5 (0索引)的长度为5的子数组[2,1,2,3,2]具有出现为3的数字2 首先想到的是一个明显的强力(

  • 假设这条线是由熊猫的离散随机数组成的。我怎样才能找到A、B、C、D点? A是第一点和C之间的最高点 C是A和B之间的最低点 B是C和D之间的最高点 您可以使用这些数据来测试:[1, 2, 3, 10, 13, 15, 20, 50, 49, 49, 32, 33, 35, 36, 35, 34, 33, 34, 35, 36, 30, 27, 22, 15, 15, 17, 20, 27, 30,