python:根据索引事件将时间间隔数据分成两天的块

huangapple go评论96阅读模式
英文:

python: sorting time interval data into two days chucks based on index event

问题

  1. 我有以下数据:

df =
id date_medication medication index_date
1 2000-01-01 A 2000-01-04
1 2000-01-02 A 2000-01-04
1 2000-01-05 B 2000-01-04
1 2000-01-06 B 2000-01-04
2 2000-01-01 A 2000-01-05
2 2000-01-03 B 2000-01-05
2 2000-01-06 A 2000-01-05
2 2000-01-10 B 2000-01-05

  1. 我想将数据转换为围绕索引事件(IE)的两天时间段。创建新的列代表时间间隔,如下:

df =
id -4 -2 0 2 4 6
1 A A IE B 0 0
2 A B IE A A B

  1. <details>
  2. <summary>英文:</summary>
  3. I have the following data:

df =
id date_medication medication index_date
1 2000-01-01 A 2000-01-04
1 2000-01-02 A 2000-01-04
1 2000-01-05 B 2000-01-04
1 2000-01-06 B 2000-01-04
2 2000-01-01 A 2000-01-05
2 2000-01-03 B 2000-01-05
2 2000-01-06 A 2000-01-05
2 2000-01-10 B 2000-01-05

  1. and I would like to transform the data into two days&#39; chucks around the index event (IE). That is creating new columns representing the time intervals such as:

df =
id -4 -2 0 2 4 6
1 A A IE B 0 0
2 A B IE A A B

  1. </details>
  2. # 答案1
  3. **得分**: 2
  4. ```python
  5. #将列转换为日期时间
  6. df['date_medication'] = pd.to_datetime(df['date_medication'])
  7. df['index_date'] = pd.to_datetime(df['index_date'])
  8. #获取2天的时间段
  9. s = df['date_medication'].sub(df['index_date']).dt.days // 2 * 2
  10. #对大于等于0的值添加2天
  11. s.loc[s.ge(0)] += 2
  12. #透视列
  13. df1 = df.assign(g=s).pivot(index='id', columns='g', values='medication')
  14. #添加0列
  15. df1.loc[:, 0] = 'IE'
  16. #添加0列
  17. df1 = (df1.rename_axis(columns=None)
  18. .reindex(columns=range(df1.columns.min(), df1.columns.max() + 2, 2), fill_value=0)
  19. .fillna(0)
  20. .reset_index())
  21. id -4 -2 0 2 4 6
  22. 0 1 A A IE B B 0
  23. 1 2 A B IE A 0 B
  24. print(s)
  25. 0 -4
  26. 1 -2
  27. 2 2
  28. 3 4
  29. 4 -4
  30. 5 -2
  31. 6 2
  32. 7 6
  33. dtype: int64
英文:

Use:

  1. #convert columns to datetimes
  2. df[&#39;date_medication&#39;] = pd.to_datetime(df[&#39;date_medication&#39;])
  3. df[&#39;index_date&#39;] = pd.to_datetime(df[&#39;index_date&#39;])
  4. #get 2 days chunks
  5. s = df[&#39;date_medication&#39;].sub(df[&#39;index_date&#39;]).dt.days // 2 * 2
  6. #add 2 days for greater/equal values 0
  7. s.loc[s.ge(0)] += 2
  8. #pivoting columns
  9. df1 = df.assign(g = s).pivot(index=&#39;id&#39;, columns=&#39;g&#39;, values=&#39;medication&#39;)
  10. #added 0 column
  11. df1.loc[:, 0] = &#39;IE&#39;
  12. #added 0 column
  13. df1 = (df1.rename_axis(columns=None)
  14. .reindex(columns=range(df1.columns.min(), df1.columns.max() + 2, 2), fill_value=0)
  15. .fillna(0)
  16. .reset_index())
  17. id -4 -2 0 2 4 6
  18. 0 1 A A IE B B 0
  19. 1 2 A B IE A 0 B

Details:

  1. print (s)
  2. 0 -4
  3. 1 -2
  4. 2 2
  5. 3 4
  6. 4 -4
  7. 5 -2
  8. 6 2
  9. 7 6
  10. dtype: int64

答案2

得分: 2

  1. # 计算差值
  2. days = df['date_medication'].sub(df['index_date']).dt.days
  3. # 创建所需的区间和标签
  4. bins = np.arange(days.min() - days.min() % 2, days.max() + days.max() % 2 + 1, 2)
  5. lbls = bins[bins != 0] # 排除0
  6. df['interval'] = pd.cut(days, bins, labels=lbls, include_lowest=True, right=False)
  7. # 重塑数据框
  8. out = (df.pivot(index='id', columns='interval', values='medication')
  9. .reindex(bins, fill_value='IE', axis=1).fillna(0)
  10. .rename_axis(columns=None).reset_index())
英文:

You can use:

  1. # Compute the delta
  2. days = df[&#39;date_medication&#39;].sub(df[&#39;index_date&#39;]).dt.days
  3. # Create desired bins and labels
  4. bins = np.arange(days.min() - days.min() % 2, days.max() + days.max() % 2 + 1, 2)
  5. lbls = bins[bins != 0] # Exclude 0
  6. df[&#39;interval&#39;] = pd.cut(days, bins, labels=lbls, include_lowest=True, right=False)
  7. # Reshape your dataframe
  8. out = (df.pivot(index=&#39;id&#39;, columns=&#39;interval&#39;, values=&#39;medication&#39;)
  9. .reindex(bins, fill_value=&#39;IE&#39;, axis=1).fillna(0)
  10. .rename_axis(columns=None).reset_index())

Output:

  1. &gt;&gt;&gt; out
  2. id -4 -2 0 2 4 6
  3. 0 1 A A IE B B 0
  4. 1 2 A B IE A 0 B

huangapple
  • 本文由 发表于 2023年3月9日 18:17:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定