Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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
python 2相当于使用pandas df获取_假人_Python_Pandas_Jupyter Notebook - Fatal编程技术网

python 2相当于使用pandas df获取_假人

python 2相当于使用pandas df获取_假人,python,pandas,jupyter-notebook,Python,Pandas,Jupyter Notebook,我希望澄清为什么我的代码不能使用以下示例数据使用虚拟值访问特定列值: df shop category subcategory season date 2013-09-04 abc weddings shoes winter 2013-09-04 def jewelry watches summer 2013-09-05 ghi sports sneak

我希望澄清为什么我的代码不能使用以下示例数据使用虚拟值访问特定列值:

df

            shop   category  subcategory     season
date                
2013-09-04  abc    weddings  shoes           winter
2013-09-04  def    jewelry   watches         summer
2013-09-05  ghi    sports    sneakers        spring
2013-09-05  jkl    jewelry   necklaces       fall
这是我的基本代码:

wedding_df = df[["weddings","winter","summer","spring","fall"]]
我在笔记本上使用的是Python 2,因此它很可能是一个版本问题,需要
get_dummies()
,但一些指导可能会有所帮助。其想法是创建一个虚拟数据帧,该数据帧使用二进制来表示一行是否有婚礼类别以及什么季节

下面是我希望实现的一个示例:

        weddings    winter  summer  spring  fall
71654   1.0         0.0     1.0     0.0     0.0
72168   1.0         0.0     1.0     0.0     0.0
72080   1.0         0.0     1.0     0.0     0.0
使用
corr()


您可以尝试使用
prefix
prefix\u sep
将它们指定为空白,然后您可以
df[[“婚礼”、“冬季”、“夏季”、“春季”、“秋季”]

更新

pd.get_dummies(df.loc[df['category']=='weddings',['category','season']],prefix = '', prefix_sep = '' )
Out[820]: 
            weddings  winter
date                        
2013-09-04         1       1

我编辑了我的帖子,展示了我想要表达的一个例子achieve@Mr.Jibz我看不懂,你能解释一下如何使用你的输入来获取你的输出吗?目的是过滤cateogray列,使其仅限于
weddings
输入,以及与
婚礼
条目相对应的
季节
值,这样我就可以有五列,
婚礼
冬季
夏季
春季
秋季
,所有这些都将通过上面显示的时间索引进行索引。@Mr.Jibz更新。。。仍然无法匹配您的输出,我想可能您没有在这里提供所有样本数据?我重新编辑了我的查询,以显示在使用
corr()
时,
weddings
列显示
NaN
值。这是因为我在
get\u dummies()
下设置参数的方式吗?它显示它是一个类似于所有
季节
专栏的专栏。。。
df = pd.get_dummies(df,prefix = '', prefix_sep = '' )
df
            abc  def  ghi  jkl  jewelry  sports  weddings  necklaces  shoes  \
date                                                                          
2013-09-04    1    0    0    0        0       0         1          0      1   
2013-09-04    0    1    0    0        1       0         0          0      0   
2013-09-05    0    0    1    0        0       1         0          0      0   
2013-09-05    0    0    0    1        1       0         0          1      0   
            sneakers  watches  fall  spring  summer  winter  
date                                                         
2013-09-04         0        0     0       0       0       1  
2013-09-04         0        1     0       0       1       0  
2013-09-05         1        0     0       1       0       0  
2013-09-05         0        0     1       0       0       0  
pd.get_dummies(df.loc[df['category']=='weddings',['category','season']],prefix = '', prefix_sep = '' )
Out[820]: 
            weddings  winter
date                        
2013-09-04         1       1