为了账号安全,请及时绑定邮箱和手机立即绑定

空dataframe过滤行之后列名丢失了

空dataframe过滤行之后列名丢失了

米脂 2019-05-12 11:14:01
一个按条件过滤pandas中行的函数,对于非空的dataframe可以正常工作,但是对于带列名的空的dataframe,过滤后的空datafram缺丢失了列名问题复现如下:In[5]:t1=pd.DataFrame(columns=['a','b'])In[6]:t2=pd.DataFrame({'a':[-1,1],'b':[2,3]})In[7]:t1Out[7]:EmptyDataFrameColumns:[a,b]Index:[]In[8]:t2Out[8]:ab0-12113In[13]:defmyfunc1(row):...:ifrow.empty:...:print(row)...:returnTrue...:ifint(row['a'])>0:...:returnTrue...:else:...:returnFalse...:In[17]:t2[t2.apply(myfunc1,axis=1)]Out[17]:ab113In[18]:t1[t1.apply(myfunc1,axis=1)]Series([],dtype:float64)Out[18]:EmptyDataFrameColumns:[]Index:[]t2结果过滤得到row['a']>0的新dataframe,但是t1经过过滤为什么丢失了columns呢?因为后续处理要用到columns,所以我想知道为什么丢失了columns
查看完整描述

2 回答

?
Helenr

TA贡献1780条经验 获得超4个赞

因为t1,t2内部的筛选条件不一样.t2内部条件其实是取第二行。
In[18]:t2.apply(myfunc,axis=1)
Out[18]:
0False
1True
dtype:bool
In[19]:t2[t2.apply(myfunc,axis=1)]
Out[19]:
ab
113
t1的内部条件则不同,没有True,False,是一个空的DataFrame
In[20]:t1.apply(myfunc,axis=1)
Out[20]:Series([],dtype:float64)
In[21]:t1[t1.apply(myfunc,axis=1)]
Series([],dtype:float64)
                            
查看完整回答
反对 回复 2019-05-12
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

感谢@everfigt
还是不懂colums怎么丢的。。。t2的情况好理解,就是按true/false过滤,也就是说把空dataframe当成内部条件是没什么意义的吧?但是我尝试直接用空dataframe也是有结果的:
In[11]:t2[pd.DataFrame()]
Out[11]:
ab
0NaNNaN
1NaNNaN
另外,下面两种空dataframe过滤为什么会得到不同的结果呢:
In[20]:t1.apply(myfunc,axis=1)
Series([],dtype:float64)
Out[20]:
EmptyDataFrame
Columns:[a,b]
Index:[]
In[21]:t1[t1.apply(myfunc,axis=1)]#为啥这里的colums丢了
Series([],dtype:float64)
In[13]:t1[pd.DataFrame()]#为啥直接失败了
---------------------------------------------------------------------------
ValueErrorTraceback(mostrecentcalllast)
in()
---->1t1[pd.DataFrame()]
~/programs/venv36/lib/python3.6/site-packages/pandas/core/frame.pyin__getitem__(self,key)
2133returnself._getitem_array(key)
2134elifisinstance(key,DataFrame):
->2135returnself._getitem_frame(key)
2136elifis_mi_columns:
2137returnself._getitem_multilevel(key)
~/programs/venv36/lib/python3.6/site-packages/pandas/core/frame.pyin_getitem_frame(self,key)
2217ifkey.values.sizeandnotis_bool_dtype(key.values):
2218raiseValueError('MustpassDataFramewithbooleanvaluesonly')
->2219returnself.where(key)
2220
2221defquery(self,expr,inplace=False,**kwargs):
~/programs/venv36/lib/python3.6/site-packages/pandas/core/generic.pyinwhere(self,cond,other,inplace,axis,level,errors,try_cast,raise_on_error)
6128other=com._apply_if_callable(other,self)
6129returnself._where(cond,other,inplace,axis,level,
->6130errors=errors,try_cast=try_cast)
6131
6132@Appender(_shared_docs['where']%dict(_shared_doc_kwargs,cond="False",
~/programs/venv36/lib/python3.6/site-packages/pandas/core/generic.pyin_where(self,cond,other,inplace,axis,level,errors,try_cast)
5889fordtincond.dtypes:
5890ifnotis_bool_dtype(dt):
->5891raiseValueError(msg.format(dtype=dt))
5892
5893cond=cond.astype(bool,copy=False)
ValueError:Booleanarrayexpectedforthecondition,notfloat64
                            
查看完整回答
反对 回复 2019-05-12
  • 2 回答
  • 0 关注
  • 1057 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号