Python 通过提取数据计算时间(单位:秒)
我有一套数据Python 通过提取数据计算时间(单位:秒),python,pandas,Python,Pandas,我有一套数据 Time1 Time2 XY40M XY35M XY5H XY45M XY30M XY20M XY1H XY2H XY1H30M XY2H 我必须以分钟为单位计算总时间 Time1+Time2 75 345 50 180 210 我如何推导出该公式?与以下公式一起使用: 另一个解决方案: a = df['Time1'].str.extract('(\d+)([MH])', expand=True) a1 = a[0].astype(int) b = df['T
Time1 Time2
XY40M XY35M
XY5H XY45M
XY30M XY20M
XY1H XY2H
XY1H30M XY2H
我必须以分钟为单位计算总时间
Time1+Time2
75
345
50
180
210
我如何推导出该公式?与以下公式一起使用:
另一个解决方案:
a = df['Time1'].str.extract('(\d+)([MH])', expand=True)
a1 = a[0].astype(int)
b = df['Time2'].str.extract('(\d+)([MH])', expand=True)
b1 = b[0].astype(int)
df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)
编辑: 用于: 另一个解决方案:
a = df['Time1'].str.extract('(\d+)([MH])', expand=True)
a1 = a[0].astype(int)
b = df['Time2'].str.extract('(\d+)([MH])', expand=True)
b1 = b[0].astype(int)
df['Time'] = np.where(a[1] == 'H', a1 * 60, a1) + np.where(b[1] == 'H', b1 * 60, b1)
编辑:
print (df)
Time1 Time2 Time
0 XY40M XY35M 75
1 XY5H XY45M 345
2 XY30M XY20M 50
3 XY1H XY2H 180
a = df['Time1'].str.extract('(\d+)([MH])(\d*)([M]*)', expand=True)
a1 = a[[0,2]].replace('', 0).astype(int)
b = df['Time2'].str.extract('(\d+)([MH])(\d*)([M]*)', expand=True)
b1 = b[[0,2]].replace('', 0).astype(int)
df['Time'] = np.where(a[1] == 'H', a1[0] * 60, a1[0]) + a1[2] + \
np.where(b[1] == 'H', b1[0] * 60, b1[0]) + b1[2]
print (df)
Time1 Time2 Time
0 XY40M XY35M 75
1 XY5H XY45M 345
2 XY30M XY20M 50
3 XY1H XY2H 180
4 XY1H30M XY2H 210