在pandas中,是否可以在diff操作中将(NULL)不存在的索引/值视为0?

2024-04-25 12:22:34 发布

您现在位置:Python中文网/ 问答频道 /正文

举例如下

d1有d1。KRK.ANDROID公司==600,而d2没有此类索引。在d2-d1中是否可以将缺少此类索引/值视为0?你知道吗

现在行动回来了。你知道吗

手动去定义这样的索引和零值是唯一的选择吗?所以我需要把d2-d1调成-600KRK.ANDROID公司而不是NaN。你知道吗

d2=pd.DataFrame({'branch':['EKB','KRK','NB','VN'],
                  'worktype':['PHP','PYTHON','PYTHON','ANDROID'],
                  'minutes':[20, 270, 20, 20]})\
      .set_index(['branch', 'worktype'])

d1=pd.DataFrame({'branch':['EKB','KRK','KRK','KRK', 'NB', 'VN'],
                  'worktype':['PHP','ANDROID','PYTHON','QA', 'PYTHON', 'ANDROID'],
                  'minutes':[20, 600, 680, 45, 120, 15]})\
      .set_index(['branch', 'worktype'])

In [293]: d2
Out[293]: 
                 minutes
branch worktype         
EKB    PHP            20
KRK    PYTHON        270
NB     PYTHON         20
VN     ANDROID        20

In [294]: d1
Out[294]: 
                 minutes
branch worktype         
EKB    PHP            20
KRK    ANDROID       600
       PYTHON        680
       QA             45
NB     PYTHON        120
VN     ANDROID        15

In [295]: d2 - d1
Out[295]: 
                 minutes
branch worktype         
EKB    PHP           0.0
KRK    ANDROID       NaN
       PYTHON     -410.0
       QA            NaN
NB     PYTHON     -100.0
VN     ANDROID       5.0

Tags: inbranchnanoutqaandroidd2d1
2条回答

你可以试试reindex:-)

d2.reindex(d1.index).fillna(0)-d1
Out[342]: 
                 minutes
branch worktype         
EKB    PHP           0.0
KRK    ANDROID    -600.0
       PYTHON     -410.0
       QA          -45.0
NB     PYTHON     -100.0
VN     ANDROID       5.0

对于您的附加要求

if len(d2.index.labels[1])<len(d1.index.labels[1]):
    print(d2.reindex(d1.index).fillna(0) - d1)
else :
    print(d2 - d1.reindex(d2.index).fillna(0))

更新2

AAA=set(d1.index.tolist()+d2.index.tolist())
d1.reindex(AAA).fillna(0)-d2.reindex(AAA).fillna(0)

我发现另一种方法是使用测向差异()当您需要查找日期/时间索引的差异时。即特定值每天如何变化。我只需要将原来的groupby数据帧转换成纯2D数据帧,而不需要使用multindex

In [912]: gp = gp.unstack(level=1).fillna(0) 
In [913]: with pd.option_context('display.max_rows', None, 'display.max_columns', 3):
     ...:     print(gp)
     ...:     
department  MOBILE  ...      WEB
period              ...         
2016-02-03     0.0  ...     30.0
2016-12-24     0.0  ...    400.0
2016-12-25     0.0  ...     80.0
2016-12-26     0.0  ...     20.0
2016-12-27     0.0  ...    180.0
2016-12-28   600.0  ...     15.0
2017-01-01     0.0  ...    190.0
2017-01-03     0.0  ...     80.0
2017-01-04    20.0  ...      0.0
2017-02-01   120.0  ...      0.0
2017-02-02    45.0  ...      0.0

In [914]: with pd.option_context('display.max_rows', None, 'display.max_columns', 3):
     ...:     print(gp.diff())
     ...:     
     ...:     
department  MOBILE  ...      WEB
period              ...         
2016-02-03     NaN  ...      NaN
2016-12-24     0.0  ...    370.0
2016-12-25     0.0  ...   -320.0
2016-12-26     0.0  ...    -60.0
2016-12-27     0.0  ...    160.0
2016-12-28   600.0  ...   -165.0
2017-01-01  -600.0  ...    175.0
2017-01-03     0.0  ...   -110.0
2017-01-04    20.0  ...    -80.0
2017-02-01   100.0  ...      0.0
2017-02-02   -75.0  ...      0.0

[11 rows x 4 columns]

相关问题 更多 >

    热门问题