Python 如何在pandas中筛选float64值

Python 如何在pandas中筛选float64值,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个csv文件,我想过滤熊猫。这就是csv文件的外观 Name Employee ID Jane Doe 00101848707829 Jason Smith 0030201689375900 Jason Bourne 0017501001410513 ... 我只想要员工ID以001开头的数据。所以过滤后的结果是这样的 Name Employee ID Jane Doe 00101848707829 J

我有一个csv文件,我想过滤熊猫。这就是csv文件的外观

Name            Employee ID
Jane Doe        00101848707829
Jason Smith     0030201689375900
Jason Bourne    0017501001410513
...
我只想要
员工ID
001
开头的数据。所以过滤后的结果是这样的

Name            Employee ID
Jane Doe        00101848707829
Jason Bourne    0017501001410513
这是我的密码

df_filter = df.loc[:, df['Employee ID'].str.startswith("890")]
问题是
Employee ID
的数据类型是
float64
。所以我得到了以下错误

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-de57783153c3> in <module>
----> 1 df_filter = df.loc[:, df['Employee ID'].str.startswith("001")]

~/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5268             or name in self._accessors
   5269         ):
-> 5270             return object.__getattribute__(self, name)
   5271         else:
   5272             if self._info_axis._can_hold_identifiers_and_holds_name(name):

~/anaconda3/lib/python3.8/site-packages/pandas/core/accessor.py in __get__(self, obj, cls)
    185             # we're accessing the attribute of the class, i.e., Dataset.geo
    186             return self._accessor
--> 187         accessor_obj = self._accessor(obj)
    188         # Replace the property with the accessor object. Inspired by:
    189         # http://www.pydanny.com/cached-property.html

~/anaconda3/lib/python3.8/site-packages/pandas/core/strings.py in __init__(self, data)
   2039 
   2040     def __init__(self, data):
-> 2041         self._inferred_dtype = self._validate(data)
   2042         self._is_categorical = is_categorical_dtype(data)
   2043         self._is_string = data.dtype.name == "string"

~/anaconda3/lib/python3.8/site-packages/pandas/core/strings.py in _validate(data)
   2096 
   2097         if inferred_dtype not in allowed_types:
-> 2098             raise AttributeError("Can only use .str accessor with string values!")
   2099         return inferred_dtype
   2100 

AttributeError: Can only use .str accessor with string values!
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
---->1 df_filter=df.loc[:,df['Employee ID'].str.startswith(“001”)]
~/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in\uuuuu getattr\uuuuu(self,name)
5268或名称在self.\u访问器中
5269         ):
->5270返回对象。\uuuu getattribute\uuuuu(self,name)
5271其他:
5272如果自身信息轴可容纳标识符且容纳名称(名称):
~/anaconda3/lib/python3.8/site-packages/pandas/core/accessor.py in\uuuuu get\uuuu(self、obj、cls)
185#我们正在访问类的属性,即Dataset.geo
186返回自存取器
-->187存取器_obj=自存取器(obj)
188#将属性替换为访问器对象。灵感来自:
189         # http://www.pydanny.com/cached-property.html
~/anaconda3/lib/python3.8/site-packages/pandas/core/strings.py in\uuuuuu init\uuuu(self,data)
2039
2040定义初始化(自身,数据):
->2041自我验证(数据)
2042 self.\u is\u categorical=is\u categorical\u数据类型(数据)
2043 self._is_string=data.dtype.name==“string”
~/anaconda3/lib/python3.8/site-packages/pandas/core/strings.py in_-validate(数据)
2096
2097如果推断的\u数据类型不在允许的\u类型中:
->2098 raise AttributeError(“只能使用带字符串值的.str访问器!”)
2099返回推断的\u数据类型
2100
AttributeError:只能对字符串值使用.str访问器!
如何使用
startswith
过滤
float64
数据?

您可以使用:

或使用:

您可以使用:

或使用:


[df['Employee ID']=[df['Employee ID'].astype(str)
然后
df_filter=df[df['Employee ID'].str.startswith(“001”)
您的语法已关闭。
[df['Employee ID']=[df['Employee ID'].astype(str)
,然后
df_filter=df[df['Employee ID']str.startswith(“001”)]
您的语法已关闭。@Sashaank如果答案有帮助,请不要忘记向上投票并接受它。@Sashaank如果答案有帮助,请不要忘记向上投票并接受它。
df_filter = df[df['Employee ID'].apply(lambda x: str(x).startswith('001'))]
df['Employee ID'] = [df['Employee ID'].astype(str)
df_filter = df[df['Employee ID'].str.startswith("001")]