[英]Select element from list in Pandas Series based on another column
I have a Pandas DataFrame of the following form:我有以下形式的 Pandas DataFrame:
Name Dates Trigger
John [d1,d2,d3] 1
Mike [d4] NaN
Li [d1,d4,d5] 2
where the rows in the column Dates are Python lists, where each element in the lists is a DateTime object (eg '2019-08-15').其中Dates列中的行是 Python 列表,其中列表中的每个元素都是 DateTime object(例如“2019-08-15”)。
My final goal is to obtain an array with the differences (in days) between the Dates at the index position mentioned in Trigger , resulting in a new column like:我的最终目标是获取一个数组,其中包含Trigger中提到的索引 position 处的日期之间的差异(以天为单位),从而产生一个新列,如:
Date_diff
[d2-d1,d2-d2,d2-d3]
[NaN]/d4
[d5-d1,d5-d4,d5-d5]
No matter what I've tried, I always failed to properly identify the correct element in the list based on the last column.无论我尝试了什么,我总是无法根据最后一列正确识别列表中的正确元素。 Any suggestions?有什么建议么?
From what I understand you want to take the Trigger as the index in the list of the element from which the other elements get subtracted.据我了解,您希望将 Trigger 作为元素列表中的索引,从中减去其他元素。 I still don't know what you're trying to say for the row corresponding to Mike.我仍然不知道您要对与 Mike 对应的行说什么。
list1
of the third column from your data frame (Trigger)从数据框中创建第三列的list1
(触发器)list2
of the second column from your data frame (Dates)从您的数据框(日期)创建第二列的list2
list3
which will be your Dates_Difference column.创建一个空的list3
这将是您的 Dates_Difference 列。list1
with a variable i
and iterate through the list2
inside it with a variable j
and keep appending list2[i] - list2[j]
to list3
.使用变量i
枚举list1
并使用变量j
遍历其中的list2
并继续将list2[i] - list2[j]
附加到list3
。 Handle the cases when i = NaN
accordingly.相应地处理i = NaN
的情况。list3
as a new column to your data frame with the name Dates_diff list3
作为新列插入到您的数据框中,名称为 Dates_diffHope it helps:)希望能帮助到你:)
After handling the NaN's, the following solution works:处理 NaN 后,以下解决方案有效:
df.apply(lambda row: row.Dates[row.Trigger], axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.