如何从字符串python pandas中提取数字和字符?

发布于 2025-02-13 07:50:35 字数 615 浏览 1 评论 0原文

我有一个混合数字数据和字符数据的数据集。我只想提取数值数据和字母“ w”(我不需要'2 x hdmi | 2 x usb'....)。

在这种情况下(20 W,30W等)。 感谢您的帮助

v=['2 x HDMI | 2 x USB', '20 W Speaker Output', '10 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output', '20 Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '30 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '2 x HDMI | 2 x USB', '20 W Speaker Output',
       '20 Speaker Output', '24 W Speaker Output', '20 W Speaker Output']

df=pd.DataFrame({"col_1":v})

I have a dataset that mixes numeric and character data. I would like to extract only the numerical data and letter "W" (i don't need '2 x HDMI | 2 x USB'....) .

for exemple in this case (20 W, 30W etc).
thank you for your help

v=['2 x HDMI | 2 x USB', '20 W Speaker Output', '10 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output', '20 Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '30 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '2 x HDMI | 2 x USB', '20 W Speaker Output',
       '20 Speaker Output', '24 W Speaker Output', '20 W Speaker Output']

df=pd.DataFrame({"col_1":v})

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

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

发布评论

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

评论(3

秋千易 2025-02-20 07:50:35

您可以使用正则表达式和一些列表理解骗局来获得所需的东西:

import re
import pandas as pd

v=['2 x HDMI | 2 x USB', '20 W Speaker Output', '10 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output', '20 Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '30 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '2 x HDMI | 2 x USB', '20 W Speaker Output',
       '20 Speaker Output', '24 W Speaker Output', '20 W Speaker Output']

df=pd.DataFrame({"col_1":[v.group(0) for v in [re.search('\d+\s?[Ww]', v) for v in v] if v]})

...导致:

>>> df
   col_1
0   20 W
1   10 W
2   20 W
3   20 W
4   20 W
5   20 W
6   20 W
7   20 W
8   30 W
9   20 W
10  20 W
11  20 W
12  24 W
13  20 W

You can use regular expressions and a little bit of list comprehension trickery to get what you desire:

import re
import pandas as pd

v=['2 x HDMI | 2 x USB', '20 W Speaker Output', '10 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output', '20 Speaker Output',
       '20 W Speaker Output', '20 W Speaker Output',
       '30 W Speaker Output', '20 W Speaker Output',
       '20 W Speaker Output', '2 x HDMI | 2 x USB', '20 W Speaker Output',
       '20 Speaker Output', '24 W Speaker Output', '20 W Speaker Output']

df=pd.DataFrame({"col_1":[v.group(0) for v in [re.search('\d+\s?[Ww]', v) for v in v] if v]})

... results in:

>>> df
   col_1
0   20 W
1   10 W
2   20 W
3   20 W
4   20 W
5   20 W
6   20 W
7   20 W
8   30 W
9   20 W
10  20 W
11  20 W
12  24 W
13  20 W
陌生 2025-02-20 07:50:35

尝试:

import re
for x in v:
    ms = re.compile(r'\d+\s[wW]')
    m = re.search(ms, x)
    print(m.group())

Try:

import re
for x in v:
    ms = re.compile(r'\d+\s[wW]')
    m = re.search(ms, x)
    print(m.group())
杯别 2025-02-20 07:50:35
print(df['col_1'].str.extract(r'(\d+ [W])*'))

上述提取方法与正则曲目,将按预期过滤,然后清除NAN值。

print(df['col_1'].str.extract(r'(\d+ [W])*'))

This above extract method with regex, will filter as expected, then clear Nan value.

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