我创建了一个Pandas数据帧,并想过滤一些值。数据帧包含4列,即currency port supplier_id value
,我希望有能够满足以下条件的值
* port – expressed as a portcode, a 5-letter string uniquely identifying a port. Portcodes consist of 2-letter country code and 3-letter city code.
* supplier_id - integer, uniquely identifying the provider of the information
* currency - 3-letter string identifying the currency
* value - a floating-point number
df = df[ (len(df['port']) == 5 & isinstance(df['port'], basestring)) & \
isinstance(df['supplier_id'], int) & \
(len(df['currency']) == 3 & isinstance(df['currency'], basestring))\
isinstance(df['value'], float) ]
代码片段应该很明显,并尝试实现前面提到的条件,但它不起作用。来自df
的打印如下所示
怎么写好?在
可以使用if在一列中包含混合值-带字符串的数值:
相关问题 更多 >
编程相关推荐