【问题标题】:Compare date present in different rows of a data-frame using Python使用 Python 比较数据框不同行中的日期
【发布时间】:2018-10-24 07:02:45
【问题描述】:

我正在从 mongodb 和 csv 文件中读取一个文档,并将它们合并以检索重复记录。我有以下代码。现在我想比较这些记录之间的日期(LastUpdate)并返回具有最新日期的行。有人可以帮忙吗?

代码:

import json
import pandas as pd
import xlrd
from pymongo import MongoClient
from functools import reduce

try: 
    client = MongoClient() 
    print("Connected successfully!!!") 
except:   
    print("Could not connect to MongoDB") 

# database 
db = client.conn
collection = db.contactReg

df = pd.DataFrame(list(collection.find()))
print(df)

df1 = df[df.duplicated(['name'], keep = False)]
print(df1)

# reading the csv file
df2 = pd.read_csv(r'C:\Users\swetha1\Desktop\rules.csv')
print(df2)

df3 = pd.merge(df1,df2,on="source")
print(df3)
print(df3.dtypes)

输出:

Connected successfully!!!
data from mongo
    LastUpdate                       _id    name  nameId  source sourceId
0  10-Oct-2018  5bbc86e5c16a27f1e1bd39f8  swetha   123.0   Blore       10
1  11-Oct-2018  5bbc86e5c16a27f1e1bd39f9  swetha   123.0   Mlore       11
2   9-Oct-2018  5bbc86e5c16a27f1e1bd39fa  swathi   124.0   Mlore       11

fetching duplicates
    LastUpdate                       _id    name  nameId  source sourceId
0  10-Oct-2018  5bbc86e5c16a27f1e1bd39f8  swetha   123.0  Blore       10
1  11-Oct-2018  5bbc86e5c16a27f1e1bd39f9  swetha   123.0  Mlore       11

reading CSV file
   source  P.weight  N.weight  Tolerance(days)  Durability(Days)
0  Blore       100      -100                0                 0
1  Mlore       200      -200               30               365

merging
    LastUpdate                       _id    name  nameId  source sourceId  
P.weight  N.weight  Tolerance(days)  Durability(Days)
0  10-Oct-2018  5bbc86e5c16a27f1e1bd39f8  swetha   123.0  Blore       10       
100      -100                0                 0
1  11-Oct-2018  5bbc86e5c16a27f1e1bd39f9  swetha   123.0  Mlore       11       
200      -200               30               365

【问题讨论】:

    标签: python-3.x pandas dataframe date-comparison


    【解决方案1】:

    先转换列to_datetime,然后按boolean indexing过滤:

    df3['LastUpdate'] = pd.to_datetime(df3['LastUpdate'])
    df4 = df3[df3['LastUpdate'] == df3['LastUpdate'].max()]
    

    或者使用idxmax

    df3['LastUpdate'] = pd.to_datetime(df3['LastUpdate'])
    df4 = df3.loc[[df3['LastUpdate'].idxmax()]]
    

    【讨论】:

    • 谢谢它的工作。如果我想根据某些条件进行比较,则扩展上述问题,那么该怎么做?例如:“如果 LastUpdate(Blore) 比 Lastupdate(Mlore) 旧,则 N.weight 为 -100。如果 LastUpdate(Mlore) 比另一个旧,则 N.weight 为 -200。你能帮忙吗?
    • @swethareddy - 不确定是否理解,第二个需要 max_Mlore_val = df3.loc[(df3['source'] == 'Mlore'), 'LastUpdate'].max() df3['N.weight'] = np.where(df3['LastUpdate'] > max_Mlore_val, -200, df3['N.weight'] ) ?
    猜你喜欢
    • 2017-11-06
    • 2021-12-17
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    相关资源
    最近更新 更多