如何仅在Python Pandas中使用3个字符的Pandas DataFrame选择行?

发布于 2025-02-12 18:39:59 字数 244 浏览 1 评论 0原文

我有下面的pandas dataframe,col1是字符串数据类型:

col1
-----
"123"
"1111"
"287777"
NaN
"222"

我只需要选择这些行,其中“ col1”中的字符串具有3个字符,因此我需要以下类似的东西:

col1:
-----
"123"
"222"

我该如何在python pandas中做到这一点?

I have Pandas DataFrame like below, col1 is STRING data type:

col1
-----
"123"
"1111"
"287777"
NaN
"222"

And I need to select only these rows where string in "col1" has 3 characters, so as a result i need something like below:

col1:
-----
"123"
"222"

How can I do that in Python Pandas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2025-02-19 18:39:59

您可以使用此任务的功能:

df[df['col1'].str.len() == 3]

输出:

    col1
0   123
4   222

You can use the pandas.Series.str.len function for this task:

df[df['col1'].str.len() == 3]

Output:

    col1
0   123
4   222
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文