简体   繁体   English

基于另一列的 Pandas 系列列表中的 Select 元素

[英]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 对应的行说什么。

  1. Create a list1 of the third column from your data frame (Trigger)从数据框中创建第三列的list1 (触发器)
  2. Create a list2 of the second column from your data frame (Dates)从您的数据框(日期)创建第二列的list2
  3. Create an empty list3 which will be your Dates_Difference column.创建一个空的list3这将是您的 Dates_Difference 列。
  4. Enumerate the 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的情况。
  5. Insert list3 as a new column to your data frame with the name Dates_diff list3作为新列插入到您的数据框中,名称为 Dates_diff

Hope 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.

相关问题 根据另一列中的值从pandas列中的列表中提取元素 - Extracting element from a list in a pandas column based on the value in another column Pandas 复制列元素并根据相关列表应用于另一列 - Pandas Copy column element and apply to another column based on related list 熊猫系列:根据其他系列中的值选择行int系列 - Pandas series: select rows int series based upon values from another series Select 列动态在 Pandas dataframe 基于列表或另一列中的值 - Select column dynamically in Pandas dataframe based on values in a list or another column 基于另一列从熊猫数据框中的列中的每个列表中删除元素 - Remove element from every list in a column in pandas dataframe based on another column 匹配两个熊猫系列:如何从另一个系列的一个系列中找到一个字符串元素,然后创建一个新列 - Matching two pandas series: How to find a string element from one series in another series and then create a new column 基于2系列和列表生成Pandas列 - Generating Pandas Column Based on 2 Series and List 如何根据来自另一个系列的条件在 Pandas 系列中创建新列 - How to create a new column in a Pandas series based off of conditions derived from another series Pandas:根据另一个列列表中的值对列列表进行排序 - Pandas: sort column lists based on values from another column list 根据另一列是否包含列表中的名称,在 Pandas 中设置一列 - Set a column in Pandas based on if another column contains a name from a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM