根据对象的类型(即str)从DataFrame中选择行 [英] Select row from a DataFrame based on the type of the object(i.e. str)

查看:95
本文介绍了根据对象的类型(即str)从DataFrame中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有一个DataFrame说:

So there's a DataFrame say:

>>> df = pd.DataFrame({
...                 'A':[1,2,'Three',4],
...                 'B':[1,'Two',3,4]})
>>> df
       A    B
0      1    1
1      2  Two
2  Three    3
3      4    4

我想选择特定列的特定行的数据类型为str的行.

I want to select the rows whose datatype of particular row of a particular column is of type str.

例如,我想选择其中列A中的数据typestr的行. 因此它应该打印如下内容:

For example I want to select the row where type of data in the column A is a str. so it should print something like:

   A      B
2  Three  3

其直观代码如下:

df[type(df.A) == str]

显然不起作用!

谢谢您的帮助!

推荐答案

这有效:

df[df['A'].apply(lambda x: isinstance(x, str))]

这篇关于根据对象的类型(即str)从DataFrame中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆