正在尝试从数据框中以逗号分隔的字符串中的日期列表中查找指定日期之前和之后最近的日期

2024-10-01 02:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

tldr;我有一个index_datein dtype: datetime64[ns] <class 'pandas.core.series.Series'>和一个list_of_dates类型<class 'list'>index_datestr格式的单个元素。将这些数据转换为相同数据类型的最佳方法是什么,这样我就可以将日期排序为index_date之前最近的日期和之后最近的日期

我有一个带有列的数据帧(df):

ID_string                   object
indexdate           datetime64[ns]
XR_count                     int64
CT_count                     int64
studyid_concat              object
studydate_concat            object
modality_concat             object

它看起来像:

    ID_string   indexdate   XR_count    CT_count    studyid_concat      studydate_concat
0   55555555    2020-09-07  10          1           ['St1', 'St5'...]       ['06/22/2019', '09/20/2020'...]
1   66666666    2020-06-07  5           0           ['St11', 'St17'...]     ['05/22/2020', '06/24/2020'...]

其中studyid_concat(“St1”)中的0元素对应于studydate_concat和情态_concat等中的0元素。出于空间原因,我没有显示情态_concat,但它类似于['XR', 'CT', ...]

我目前的目标是找到最接近的X射线研究之前和之后我的索引,以及能够排名研究从最近到最远。我对熊猫有些陌生,但以下是我目前的尝试:

df = pd.read_excel(path_to_excel, sheet_name='Sheet1')

# Convert comma separated string from Excel to lists of strings
df.studyid_concat = df.studyid_concat.str.split(',')
df.studydate_concat = df.studydate_concat.str.split(',')
df.modality_concat = df.modality_concat.str.split(',')

for x in in df['ID_string'].values:
    index_date = df.loc[df['ID_string'] == x, 'indexdate']

    # Had to use subscript [0] below because result of above was a list in an array
    studyid_list = df.loc[df['ID_string'] == x, 'studyid_concat'].values[0]
    date_list = df.loc[df['ID_string'] == x, 'studydate_concat'].values[0]
    modality_list = df.loc[df['ID_string'] == x, 'modality_concat'].values[0]

    xr_date_list = [date_list[x] for x in range(len(date_list)) if modality_list[x]=="XR"]
    xr_studyid_list = [studyid_list[x] for x in range(len(studyid_list)) if modality_list[x]=="XR"]

这就是我所能做到的,因为我对这里的数据类型有些困惑。我的indexdate当前在dtype: datetime64[ns] <class 'pandas.core.series.Series'>中,我正在考虑使用datetime模块转换它,但很难弄清楚如何转换。我也不确定我是否需要。Myxr_study_list是包含格式为“mm/dd/yyyy”的日期的字符串列表。我想,如果我能以正确的格式获取数据类型,那么我就可以解决其余的问题。如果日期是>;=或<indexdate排序为before/after,然后用indexdate减去每个日期并排序。我想无论我用我的xr_date_list做什么,我都必须确保用xr_studyid_list做同样的事情来跟踪唯一的研究id

编辑:所需的输出数据帧看起来像

    ID_string   indexdate   StudyIDBefore           StudyDateBefore     
0   55555555    2020-09-07  ['St33', 'St1', ...]    [2020-09-06, 2019-06-22, ...]
1   66666666    2020-06-07  ['St11', 'St2', ...]    [2020-05-22, 2020-05-01, ...]

其中“before”变量将从最近到最远排序,类似的“after”列将存在。我目前的目标只是检查在该索引前后的3天内是否存在研究,但如果我需要开始查看最近的研究,拥有上述数据框将给我灵活性


Tags: 数据iniddfdatestringindexlist
1条回答
网友
1楼 · 发布于 2024-10-01 02:34:47

我想我在花了一些时间思考并参考了更多关于熊猫的datetime文档后找到了自己的答案。基本上意识到我可以使用pd.to_datetime将我的字符串日期列表转换为

date_list = pd.to_datetime(df.loc[df['ID_string'] == x, 'studydate_concat'].values[0]).values

然后可以从这个列表中减去我的索引日期。我选择在一个临时的数据框中这样做,这样我就可以跟踪其他列的值(比如研究ID、模态等)

完整代码如下:

for x in df['ID_string'].values:
    index_date = df.loc[df['ID_string'] == x, 'indexdate'].values[0]
    date_list = pd.to_datetime(df.loc[df['ID_string'] == x, 'studydate_concat'].values[0]).values
    modality_list = df.loc[df['ID_string'] == x, 'modality_concat'].values[0]
    studyid_list = df.loc[df['ID_string'] == x, '_concat'].values[0]

    tempdata = list(zip(studyid_list, date_list, modality_list))
    tempdf = pd.DataFrame(tempdata, columns=['studyid', 'studydate', 'modality'])

    tempdf['indexdate'] = index_date
    tempdf['timedelta'] = tempdf['studydate']-tempdf['index_date']

    tempdf['study_done_wi_3daysbefore'] = np.where((tempdf['timedelta']>=np.timedelta64(-3,'D')) & (tempdf['timedelta']<np.timedelta64(0,'D')), True, False)
    tempdf['study_done_wi_3daysafter'] = np.where((tempdf['timedelta']<=np.timedelta64(3,'D')) & (tempdf['timedelta']>=np.timedelta64(0,'D')), True, False)
    tempdf['study_done_onindex'] = np.where(tempdf['timedelta']==np.timedelta64(0,'D'), True, False)

    XRonindex[x] = True if len(tempdf.loc[(tempdf['study_done_onindex']==True) & (tempdf['modality']=='XR'), 'studyid'])>0 else False
    XRwi3days[x] = True if len(tempdf.loc[(tempdf['study_done_wi_3daysbefore']==True) & (tempdf['modality']=='XR'), 'studyid'])>0 else False
    # can later map these values back to my original dataframe as a new column

相关问题 更多 >