Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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
Pandas 从序列中提取值_Pandas - Fatal编程技术网

Pandas 从序列中提取值

Pandas 从序列中提取值,pandas,Pandas,我想补充一点 我从列中获取所有值: from collections import Counter coun_ = set(train_df['time1'].dt.hour) 然后我向数据框添加新列,并填充默认值: for i in coun_: train_df['hour'+str(i)] = 0 现在我想从time1中获取小时数,并将1设置为右列。例如,如果time1等于10,那么我将1放入hour10。我有几种方法都没有成功,其中之一就是 for hour in [trai

我想补充一点

我从列中获取所有值:

from collections import Counter
coun_ = set(train_df['time1'].dt.hour)
然后我向数据框添加新列,并填充默认值:

for i in coun_:
    train_df['hour'+str(i)] = 0
现在我想从
time1
中获取小时数,并将
1
设置为右列。例如,如果
time1
等于10,那么我将1放入
hour10
。我有几种方法都没有成功,其中之一就是

for hour in [train_df]:
    hour['hour' + hour['time1'].dt.hour.to_string()] = 1
问题是如何从序列中提取唯一值并对其进行压缩?

与adn一起使用附加到原始值:


您能添加一些数据样本、3 4个
train_df['time1']
值和预期输出吗?@jezrael
train_df['time1'][:3]
返回会话\u id 1 2014-02-20 10:02:45 2 2014-02-22 11:19:50 2013-12-16 16:40:17谢谢,我的解决方案行了吗?或者预期的输出是什么?对不起,你能解释一下我如何在数据帧中写入这些值吗?我问这个问题是因为熊猫的第一条规则是我这样做
one\u hot=pd.get\u dummies(train\u df['time1'].dt.hour)train\u df=train\u df.join(one\u hot)
也许你知道为新列设置自定义名称吗?@rnd-当然,给我一点时间。非常感谢!!
df = df.join(pd.get_dummies(train_df['time1'].dt.hour).add_prefix('hour'))