Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算字符串列的总数_Python_Pandas - Fatal编程技术网

Python 计算字符串列的总数

Python 计算字符串列的总数,python,pandas,Python,Pandas,如何计算熊猫中字符串列的总数 myl=[('2012-11-07 19:16:07', ' 2012-11-07 19:21:07', ' 0h 05m 00s'), ('2012-11-13 06:16:07', ' 2012-11-13 06:21:07', ' 0h 05m 00s'), ('2012-11-15 09:56:07', ' 2012-11-15 11:41:07', ' 1h 45m 00s'), ('2012-11-15 22:26:07', ' 2012-11-1

如何计算熊猫中字符串列的总数

myl=[('2012-11-07 19:16:07', ' 2012-11-07 19:21:07', ' 0h 05m 00s'),
 ('2012-11-13 06:16:07', ' 2012-11-13 06:21:07', ' 0h 05m 00s'),
 ('2012-11-15 09:56:07', ' 2012-11-15 11:41:07', ' 1h 45m 00s'),
 ('2012-11-15 22:26:07', ' 2012-11-16 07:01:07', ' 8h 35m 00s')]

import pandas as pd
df = pd.DataFrame(myl, columns=['from', 'to', 'downtime'])
上述代码将在单个列中返回“停机时间”。如何计算该列中整数值的总和

In [5]: df
Out[5]:
                  from                    to     downtime
0  2012-11-07 19:16:07   2012-11-07 19:21:07   0h 05m 00s
1  2012-11-13 06:16:07   2012-11-13 06:21:07   0h 05m 00s
2  2012-11-15 09:56:07   2012-11-15 11:41:07   1h 45m 00s
3  2012-11-15 22:26:07   2012-11-16 07:01:07   8h 35m 00s
例如,在上述输出中,预计总停机时间列为9h 90m 00s


更新:

我如何计算日间停机时间

预期结果:

2012-11-07 0h 05m 00s
2012-11-13 0h 05m 00s
2012-11-15 10h 20m 00s
这是预期的工作:

df['downtime_t'] = pd.to_timedelta(df['downtime'])

df['year'] = pd.DatetimeIndex(pd.to_datetime(df['from'])).year
df['month'] = pd.DatetimeIndex(pd.to_datetime(df['from'])).month
df['day'] = pd.DatetimeIndex(pd.to_datetime(df['from'])).day

df.groupby(['year', 'month', 'day'])['downtime_t'].sum()
这也适用于年度分组:

df['from_d2'] = pd.to_datetime(df['from'])
df.groupby(df['from_d2'].map(lambda x:  x.year ))['downtime_t'].sum()
但这不起作用:

df.groupby(df['from_d2'].map(lambda x:  x.year, x.month, x.day))['downtime_t'].sum()

是否有其他方法实现按总数分组?

您可以使用pandas的
来实现时间增量功能


您想要的正是这个结果,还是
10h30m00s
也很好?(还是更好?)10h 30m 00s更好更正确!您应该首先将日期列转换为
datetimes
,将停机时间列转换为timedelta,然后只需执行
df.groupby(df['from'].dt.date()).mean()
对不起,我是
df['from'].dt.date
,没有起作用的parantises(属性而不是方法)。谢谢。出现错误#ValueError:无法为[0h 05m 00s]创建timedelta字符串转换器#pandas版本为“0.14.1”。0.15中的timedelta处理功能有很多增强(引入了timedelta标量和TimedeltaIndex)。您可能需要更新。是的。它适用于版本0.15更新的问题。
pd.to_timedelta(df['downtime']).sum()