【问题标题】:Pandas: multiply row with constant based on row index熊猫:根据行索引将行与常量相乘
【发布时间】:2022-12-12 23:26:57
【问题描述】:

我有以下数据框:

Time           00:00  ...     23:00
Date                  ...          
2022-08-01  0.000000  ...  0.249884
2022-08-02  0.177274  ...  0.846479
2022-08-03  0.275984  ...  0.333089
2022-08-04  0.206237  ...  0.295780
2022-08-05  0.138474  ...  0.163897
...

如何根据日期(索引列)乘以行值?例如 - 对于从 8 月 1 日到 3 日的范围 - 乘以 2;对于从 8 月 4 日到数据框最后一行的范围 - 3。 我应该按照这里的解决方案: link to the article on slackoverflow

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    以下是将范围内的值相乘的示例代码:

    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame(np.random.randint(0,100,size=10),
                      columns=["col1"],
                      index=pd.date_range("20221212", periods=10))
    

    现在从 12 到 16 乘以 2:

    df['2022-12-12' :'2022-12-16']['col1'] *=  2 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多