我有一个df,如下所示
previous_detail = nil
journal.journal_details.each do |detail|
if previous_detail
# do processing with current and prevous details
else
# do progessing with current detail and journal
end
previous_detail = detail
end
我正在尝试将每一行的列类型个性化,以保持数据
email | date | type
_________________________
xy@xy.com | 6/1 | order
xy@xy.com | 6/1 | return
cd@xy.com | 6/2 | return
ab@xy.com | 6/2 | return
我一直在尝试使用email | date | order | return
_________________________________
xy@xy.com | 6/1 | 1 | 0
xy@xy.com | 6/1 | 0 | 1
cd@xy.com | 6/2 | 0 | 0
ab@xy.com | 6/2 | 0 | 0
,但是输出似乎不是我想要的。从Pandas dataframe transpose with original row and column values
答案 0 :(得分:2)
您应该看看如何从分类列中创建虚拟变量。
有一个很好的Pandas函数可以实现名为“ get_dummies”的功能:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html
df.drop('type', 1).join(pd.get_dummies(df['type']))
email date order return
0 xy@xy.com 6/1 1 0
1 xy@xy.com 6/1 0 1
2 cd@xy.com 6/2 0 1
3 ab@xy.com 6/2 0 1