[英]How to make a dataframe of 0 and 1 such that each unique value is a column?
I have the following df
我有以下df
Then I would like to transform df
into df1
whose column are unique values of df
.然后我想将df
转换为df1
,其列是df
的唯一值。 Take the first row of df
as an example, its year
is 2012 and its report
is 4. then column year_2012
and column reports_4
take value 1. The other columns take value 0.以df
的第一行为例,它的year
是2012,它的report
是4,那么year_2012
列和reports_4
列取值为1,其他列取值为0。
import pandas as pd
data = {'year': [2012, 2012, 2013, 2014, 2014],
'reports': [4, 24, 31, 2, 3]}
df = pd.DataFrame(data)
Could you please elaborate how to obtain such result?您能否详细说明如何获得这样的结果?
This is pd.get_dummies
:这是pd.get_dummies
:
cols = ['year', 'reports']
pd.concat([pd.get_dummies(df[col], prefix=col) for col in cols],
axis=1)
Or even easier, credit goes to @ScottBoston :或者更简单,归功于@ScottBoston :
pd.get_dummies(df, columns=df.columns)
Output: Output:
year_2012 year_2013 year_2014 reports_2 reports_3 reports_4 reports_24 reports_31
0 1 0 0 0 0 1 0 0
1 1 0 0 0 0 0 1 0
2 0 1 0 0 0 0 0 1
3 0 0 1 1 0 0 0 0
4 0 0 1 0 1 0 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.