如何使用 pandas 使用条件日期时间创建新列 [英] How to create new column with conditional datetime using pandas
本文介绍了如何使用 pandas 使用条件日期时间创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加一个包含带有以下条件的标签的新列:
I am trying to add a new column containing label with this condition:
- 如果时间中的值之间的时间差为增量,则标签1和dt< 2小时
- 为其他情况贴标签0
我当前的想法:
df = pd.read_csv('./datetimecek.csv')
df['time'] = pd.to_datetime(df['datetime'])
dt = datetime.strptime("19/02/18 19:00", "%d/%m/%y %H:%M")
datetime time
2018/02/19 16:00 2018-02-19 16:00:00
2018/02/19 17:00 2018-02-19 17:00:00
2018/02/19 18:00 2018-02-19 18:00:00
2018/02/19 19:00 2018-02-19 19:00:00
然后我定义了时间增量
a = timedelta(hours=2)
def label(c):
if dt - df['time'] < a:
return '1'
else:
return '0'
然后
df['label'] = df.apply(label, axis=1)
但是我得到了一个错误:系列的真值是模棱两可的。使用a.empty,a.bool()...
But I got error: 'The truth value of a Series is ambiguous. Use a.empty, a.bool()...
反正我可以解决此问题吗?
Is there anyway I can fix this?
推荐答案
如果要设置字符串 0
和 1
:
df['label'] = np.where(dt - df['time'] < a, '1','0')
或由@Dark替代:
df['label'] = (dt - df['time'] < a).astype(int).astype(str)
print (df)
datetime time label
0 2018/02/19 16:00 2018-02-19 16:00:00 0
1 2018/02/19 17:00 2018-02-19 17:00:00 0
2 2018/02/19 18:00 2018-02-19 18:00:00 1
3 2018/02/19 19:00 2018-02-19 19:00:00 1
print (type(df.loc[0, 'label']))
<class 'str'>
如果要设置整数 0
和 1
:
df['label'] = (dt - df['time'] < a).astype(int)
替代:
df['label'] = np.where(dt - df['time'] < a, 1,0)
print (df)
datetime time label
0 2018/02/19 16:00 2018-02-19 16:00:00 0
1 2018/02/19 17:00 2018-02-19 17:00:00 0
2 2018/02/19 18:00 2018-02-19 18:00:00 1
3 2018/02/19 19:00 2018-02-19 19:00:00 1
print (type(df.loc[0, 'label']))
<class 'numpy.int32'>
我可以解决这个问题吗?
Is there anyway I can fix this?
是,需要将 df
更改为 c
用于处理标量:
Yes, need change df
to c
for working with scalars:
def label(c):
if dt - c['time'] < a:
return '1'
else:
return '0'
这篇关于如何使用 pandas 使用条件日期时间创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文