比较熊猫中的两个列与数据框中的更新日期列

发布于 2025-02-12 02:55:46 字数 449 浏览 1 评论 0原文

我需要将列的一定范围与另一列中的特定字符串进行比较,以用1,7、30天更新日期列,

Col A    ColB           colc
5          Internal     1-1-2022
7          external     1-1-2022
4          external     1-1-2022
3          external     1-1-2022

因此,如果df [cola]< 8df [colb] =外部向DF [Colc]添加7天。如果df [cola]> = 8 and df [colb] =外部将30天添加到df [colc],以及df [colb] =内部将120天添加到DF [COLC]

I need to compare a certain range of value of a column with a specific string in another column to update the date column with 1,7, 30 days

Col A    ColB           colc
5          Internal     1-1-2022
7          external     1-1-2022
4          external     1-1-2022
3          external     1-1-2022

so if df[ColA]<8 and df[ColB]=External add 7 days to df[colc]. if if df[ColA]>=8 and df[ColB]=External add 30 days to df[colc] and if df[ColB]=Internal add 120 days to df[Colc]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2025-02-19 02:55:46

您可以使用 numpy.select.select 处理所有条件:

我将它们简化为:

  • 如果COLB为“内部” - &GT;加上120天
  • ,如果可乐≤8-&gt;加7天
  • - &gt;添加30天的
factor = np.select([df['ColB'].eq('Internal'), df['Col A'].le(8)], [120, 7], 30)
df['new'] = (pd.to_datetime(df['colc'], dayfirst=True)
               .add(pd.DateOffset(days=1)*a)
               .dt.strftime('%-d-%-m-%-Y')
             )

输出:

   Col A      ColB      colc       new
0      5  Internal  1-1-2022  3-1-2022
1      7  external  1-1-2022  6-1-2022
2      4  external  1-1-2022  1-1-2022
3      3  external  1-1-2022  3-1-2022

You can use numpy.select to handle all your conditions:

I simplified them to:

  • if ColB is "Internal" -> add 120 days
  • else, if ColA is ≤ 8 -> add 7 days
  • else -> add 30 days
factor = np.select([df['ColB'].eq('Internal'), df['Col A'].le(8)], [120, 7], 30)
df['new'] = (pd.to_datetime(df['colc'], dayfirst=True)
               .add(pd.DateOffset(days=1)*a)
               .dt.strftime('%-d-%-m-%-Y')
             )

output:

   Col A      ColB      colc       new
0      5  Internal  1-1-2022  3-1-2022
1      7  external  1-1-2022  6-1-2022
2      4  external  1-1-2022  1-1-2022
3      3  external  1-1-2022  3-1-2022
国产ˉ祖宗 2025-02-19 02:55:46

使用相同的np.Select与@mozway,我建议使用pd.timedelta

df = pd.DataFrame(
    {
        'Col A': [5, 7, 4, 3], 
        'ColB': ['Internal', 'external', 'external', 'external'], 
        'colc': pd.to_datetime("1/1/2022", format="%m/%d/%Y")
    }
)

df['colc'] = [
    (a + pd.Timedelta(b, 'd')) for (a, b) in
    zip(df.colc, np.select([df['ColB'].eq('Internal'), df['Col A'].le(8)], [120, 7], 30))
]

print(df)

结果:

   Col A      ColB       colc
0      5  Internal 2022-05-01
1      7  external 2022-01-08
2      4  external 2022-01-08
3      3  external 2022-01-08

Using the same np.select as @mozway, I would suggest using pd.Timedelta.

df = pd.DataFrame(
    {
        'Col A': [5, 7, 4, 3], 
        'ColB': ['Internal', 'external', 'external', 'external'], 
        'colc': pd.to_datetime("1/1/2022", format="%m/%d/%Y")
    }
)

df['colc'] = [
    (a + pd.Timedelta(b, 'd')) for (a, b) in
    zip(df.colc, np.select([df['ColB'].eq('Internal'), df['Col A'].le(8)], [120, 7], 30))
]

print(df)

Result:

   Col A      ColB       colc
0      5  Internal 2022-05-01
1      7  external 2022-01-08
2      4  external 2022-01-08
3      3  external 2022-01-08
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文