我正在尝试计算failures
之间的天数。我想知道系列中每一天自上次failure
到failure = 1
以来经过的天数。可能有1到1500个设备。
例如,让我的数据框看起来像这样(请从第二个代码块的url中提取数据。这只是较大数据框的简短示例。)
date device failure elapsed
10/01/2015 S1F0KYCR 1 0
10/07/2015 S1F0KYCR 1 7
10/08/2015 S1F0KYCR 0 0
10/09/2015 S1F0KYCR 0 0
10/17/2015 S1F0KYCR 1 11
10/31/2015 S1F0KYCR 0 0
10/01/2015 S8KLM011 1 0
10/02/2015 S8KLM011 1 2
10/07/2015 S8KLM011 0 0
10/09/2015 S8KLM011 0 0
10/11/2015 S8KLM011 0 0
10/21/2015 S8KLM011 1 20
示例代码:
编辑:请从下面的代码块中提取实际数据。以上示例数据是一个简短的示例。谢谢。
url = "https://raw.githubusercontent.com/dsdaveh/device-failure-analysis/master/device_failure.csv"
df = pd.read_csv(url, encoding = "ISO-8859-1")
df = df.sort_values(by = ['date', 'device'], ascending = True) #Sort by date and device
df['date'] = pd.to_datetime(df['date'],format='%Y/%m/%d') #format date to datetime
这是我遇到障碍的地方。但是,新列应包含自上次failure
起的天数,其中failure = 1
。
test['date'] = 0
for i in test.index[1:]:
if not test['failure'][i]:
test['elapsed'][i] = test['elapsed'][i-1] + 1
我也尝试过
fails = df[df.failure==1]
fails.Dates = trues.index #need this because .diff() won't work on the index..
fails.Elapsed = trues.Dates.diff()
答案 0 :(得分:4)
将pandas.DataFrame.groupby
与diff
和apply
结合使用:
import pandas as pd
import numpy as np
df['date'] = pd.to_datetime(df['date'])
s = df.groupby(['device', 'failure'])['date'].diff().dt.days.add(1)
s = s.fillna(0)
df['elapsed'] = np.where(df['failure'], s, 0)
输出:
Date Device Failure Elapsed
0 2015-10-01 S1F0KYCR 1 0.0
1 2015-10-07 S1F0KYCR 1 7.0
2 2015-10-08 S1F0KYCR 0 0.0
3 2015-10-09 S1F0KYCR 0 0.0
4 2015-10-17 S1F0KYCR 1 11.0
5 2015-10-31 S1F0KYCR 0 0.0
6 2015-10-01 S8KLM011 1 0.0
7 2015-10-02 S8KLM011 1 2.0
8 2015-10-07 S8KLM011 0 0.0
9 2015-10-09 S8KLM011 0 0.0
10 2015-10-11 S8KLM011 0 0.0
11 2015-10-21 S8KLM011 1 20.0
更新:
发现OP中链接的实际数据包含具有两个以上 failure 情况的 No 设备,从而使最终结果全为零(即从未发生过第二次故障)发生,因此无法计算已过)。使用OP的原始代码段:
import pandas as pd
url = "http://aws-proserve-data-science.s3.amazonaws.com/device_failure.csv"
df = pd.read_csv(url, encoding = "ISO-8859-1")
df = df.sort_values(by = ['date', 'device'], ascending = True)
df['date'] = pd.to_datetime(df['date'],format='%Y/%m/%d')
查找是否有多个设备发生故障:
df.groupby(['device'])['failure'].sum().gt(1).any()
# False
实际上可以确认df['elapsed']
中的全零实际上是正确的答案:)
如果您稍微调整一下数据,它的确会达到预期的已用。
df.loc[6879, 'device'] = 'S1F0RRB1'
# Making two occurrence of failure for device S1F0RRB1
s = df.groupby(['device', 'failure'])['date'].diff().dt.days.add(1)
s = s.fillna(0)
df['elapsed'] = np.where(df['failure'], s, 0)
df['elapsed'].value_counts()
# 0.0 124493
# 3.0 1
答案 1 :(得分:0)
这是一种方法
df['elapsed']=df[df.Failure.astype(bool)].groupby('Device').Date.diff().dt.days.add(1)
df.elapsed.fillna(0,inplace=True)
df
Out[225]:
Date Device Failure Elapsed elapsed
0 2015-10-01 S1F0KYCR 1 0 0.0
1 2015-10-07 S1F0KYCR 1 7 7.0
2 2015-10-08 S1F0KYCR 0 0 0.0
3 2015-10-09 S1F0KYCR 0 0 0.0
4 2015-10-17 S1F0KYCR 1 11 11.0
5 2015-10-31 S1F0KYCR 0 0 0.0
6 2015-10-01 S8KLM011 1 0 0.0
7 2015-10-02 S8KLM011 1 2 2.0
8 2015-10-07 S8KLM011 0 0 0.0
9 2015-10-09 S8KLM011 0 0 0.0
10 2015-10-11 S8KLM011 0 0 0.0
11 2015-10-21 S8KLM011 1 20 20.0