熊猫系列上的字符串函数

时间:2018-04-17 13:34:07

标签: python string pandas series

我想将下面的字符串函数text.lower用于Pandas系列而不是文本文件。尝试了不同的方法将系列转换为列表然后字符串,但没有运气。我仍然无法直接使用以下功能。非常感谢帮助。

def words(text): 
    return re.findall(r'\w+', text.lower())
WORDS = Counter(words(open('some.txt').read()))

2 个答案:

答案 0 :(得分:0)

我觉得你的功能需要apply

s = pd.Series(['Aasa dsad d','GTH rr','SSD'])
print (s)
0    Aasa dsad d
1         GTH rr
2            SSD
dtype: object

def words(text): 
    return re.findall(r'\w+', text.lower())

print (s.apply(words))
0    [aasa, dsad, d]
1          [gth, rr]
2              [ssd]
dtype: object

但在熊猫中最好使用str.lowerstr.findall,因为还要使用NaN s:

print (s.str.lower().str.findall(r'\w+'))
0    [aasa, dsad, d]
1          [gth, rr]
2              [ssd]
dtype: object

答案 1 :(得分:0)

这样的东西?

<!DOCTYPE html>
<html>

  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>

  <body>

    <h1>Animal Collection</h1>
    <p>Get a look at all the species!</p>

    <div class="outer">
      <div class="box">
        <img src="http://www.owlsecurity.com.mx/wp-content/uploads/2018/02/buho.png" class="zoom centerImg">
        <div class="ttl">OWL</div>
        <div class="desc">
          <div class="wrapper"> The owl is a very fascinating species that is nocturnal. It is considered as a predator</div>
        </div>
      </div>
      <div class="box">
        <img src="https://i.pinimg.com/originals/70/a1/58/70a1580630e2233978c0755f7215a3a2.png" class="zoom centerImg">
        <div class="ttl">CAT</div>
        <div class="desc"><div class="wrapper"> The cat or feline is a descendant of a lion. It can jump as high as 13 feet </div></div>
      </div>
      <div class="box">
        <img src="https://s-media-cache-ak0.pinimg.com/originals/63/d7/3a/63d73ab34ef22437a2525e0e715f68c8.png" class="zoom centerImg">
        <div class="ttl">TOUCAN</div>
        <div class="desc"><div class="wrapper"> The Toucan is an exotic bird that comes from Brazil. It eats fruits and vegetables and is considered a herbivore.</div></div>
      </div>
      <div class="box">
        <img src="https://s-media-cache-ak0.pinimg.com/originals/63/d7/3a/63d73ab34ef22437a2525e0e715f68c8.png" class="zoom centerImg">
        <div class="ttl">TOUCAN</div>
        <div class="desc"><div class="wrapper"> The Toucan is an exotic bird that comes from Brazil. It eats fruits and vegetables and is considered a herbivore.</div></div>
        
      </div>
    </div>
    


  </body>

</html>