Python使用条件逻辑合并行

时间:2020-03-25 19:03:05

标签: python pandas dataframe

Python的新手,请原谅不好的发音。我在我应用drop_duplicates的数据框中有一些数据,以识别项目中的状态变化。数据如下所示。我的目标是在商品ID上建立一些老化。 (请注意,特定项目ID的所有记录上的“创建日期”都是相同的)。 我已经对其进行了编辑,以显示我已经尝试了什么以及得到的结果。

    Item Id      State  Created Date     Date         Severity
0    327863        New   2019-02-11    2019-10-03         1
9    327863   Approved   2019-02-11    2019-12-05         1
12   327863  Committed   2019-02-11    2019-12-26         1
16   327863       Done   2019-02-11    2020-01-23         1
27   327864        New   2019-02-11    2019-10-03         1
33   327864  Committed   2019-02-11    2019-11-14         1
42   327864       Done   2019-02-11    2020-01-16         1
53   341283   Approved   2019-03-11    2019-10-03         1
57   341283       Done   2019-03-11    2019-10-31         1

我正在执行以下操作以合并行。

s = dfdr.groupby(['Item Id','Created Date', 'Severity']).cumcount()
df1 = dfdr.set_index(['Item Id','Created Date', 'Severity', s]).unstack().sort_index(level=1, axis=1)

df1=df1.reset_index()
print(df1[['Item Id', 'Created Date', 'Severity', 'State','Date']])

输出看起来对我来说是要告诉我避免链接索引。

       Item Id            Created Date   Severity      State                                   Date
                                                           0          1          2     3          0          1          2          3
0       194795 2018-09-18 16:11:25.330        3.0        New   Approved  Committed  Done 2019-10-03 2019-10-10 2019-10-17 2019-10-24
1       194808 2018-09-18 16:11:25.330        3.0  Duplicate        NaN        NaN   NaN 2019-10-03        NaT        NaT        NaT
2       270787 2018-11-27 15:55:02.207        1.0        New  Duplicate        NaN   NaN 2019-10-03 2019-10-10        NaT        NaT

要在绘图中使用数据,我相信我想要的不是嵌套数据,而是类似以下内容的东西,但不确定如何到达那里。

Item Id    Created Date   Severity   New   NewDate      Approved      AppDate   Committed   CommDate   Done   Done Date
123456     3/25/2020         3       New   2019-10-03   Approved   2019-11-05         NaN        NaT   Done  2020-02-17

在每个Sikan Answer中添加ivot_table和reset_index之后,我离得很近,但没有得到相同的输出。这是我得到的输出。

State                                             Approved  Committed       Done  Duplicate        New
Item Id      Created Date            Severity                                                       
194795       2018-09-18              3.0        2019-10-10 2019-10-17 2019-10-24        NaT 2019-10-03
194808       2018-09-18              3.0               NaT        NaT        NaT 2019-10-03        NaT

这是我现在的代码

df = pd.read_excel(r'C:\Users\xxx\Documents\Excel\DataSample.xlsx')
df = df.drop_duplicates(subset=['Item Id', 'State','Created Date'], keep='first')
df['Severity'] = df['Severity'].replace(np.nan,3)
df = pd.pivot_table(df, index=['Item Id', 'Created Date', 'Severity'], columns=['State'], values='Date', aggfunc=lambda x: x)
df.reset_index()
print(df)

这是输出

State                                     Approved  Committed       Done  Duplicate        New
Item Id      Created Date    Severity                                                       
194795       2018-09-18      3.0        2019-10-10 2019-10-17 2019-10-24        NaT 2019-10-03     
194808       2018-09-18      3.0               NaT        NaT        NaT 2019-10-03        NaT
270787       2018-11-27      1.0               NaT        NaT        NaT 2019-10-10 2019-10-03

谢谢

1 个答案:

答案 0 :(得分:0)

您可以为此使用pd.pivot_table:

df = pd.pivot_table(dfdr, index=['Item Id', 'Created Date', 'Severity'], columns=['State'], values='Date', aggfunc=lambda x: x)
df = df.reset_index()

输出:

    ItemId  CreatedDate     Severity    Approved    Committed   Done        New
0   327863  2019-02-11      1           2019-12-05  2019-12-26  2020-01-23  2019-10-03
1   327864  2019-02-11      1           NaN         2019-11-14  2020-01-16  2019-10-03
2   341283  2019-03-11      1           2019-10-03  NaN         2019-10-31  NaN
相关问题