我有以下数据框df
:
name event_time
------------------------------------------
Mary [(S, 2017-12-03T03:40:20.000Z), (V, 2017-12-07T02:51:32.000Z)]
Peter [(S, 2017-11-02T01:11:10.000Z), (V, 2017-11-19T07:23:12.000Z)]
Andy [(S, 2017-12-01T10:31:15.000Z), (V, 2017-12-09T12:31:10.000Z)]
然后,我使用以下代码在
event_time
字段中找到两个元素的持续时间:df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])
但是,出现以下错误:
TypeError unsupported operand type(s) for -: 'unicode' and 'unicode'
TypeErrorTraceback (most recent call last)
<ipython-input-7-7a191c6f2678> in <module>()
----> 1 df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2218 else:
2219 values = self.asobject
-> 2220 mapped = lib.map_infer(values, f, convert=convert_dtype)
2221
2222 if len(mapped) and isinstance(mapped[0], Series):
pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:62658)()
<ipython-input-7-7a191c6f2678> in <lambda>(x)
----> 1 df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])
TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'
知道我在这里做错了什么吗?谢谢!
最佳答案
我相信您需要通过str[]
选择值,将to_datetime
转换并减去:
s1 = pd.to_datetime(df['event_time'].str[1].str[1])
s2 = pd.to_datetime(df['event_time'].str[0].str[1])
df['duration'] = s1 - s2
print (df)
name event_time duration
0 Mary [(S, 2017-12-03T03:40:20.000Z), (V, 2017-12-07... 3 days 23:11:12
1 Peter [(S, 2017-11-02T01:11:10.000Z), (V, 2017-11-19... 17 days 06:12:02
2 Andy [(S, 2017-12-01T10:31:15.000Z), (V, 2017-12-09... 8 days 01:59:55
关于python - Pandas :应用函数:TypeError:-不支持的操作数类型-:“unicode”和“unicode”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47747703/