Python 如何访问数据帧中的特定行

Python 如何访问数据帧中的特定行,python,pandas,Python,Pandas,我用Pandas创建了一个3列数据框,我只是尝试访问特定行的内容(里面有一个字符串) 我原以为推特['text',0]或推特.text[0]会有用,但如果你正在寻找一种你可以使用的特殊钉住,情况就不是这样了。其工作原理如下: 获取数据 import pandas as pd from io import StringIO data = """ id tweet 12 "this is the first tweet" 34 "this is the second tweet" 48 "t

我用Pandas创建了一个3列数据框,我只是尝试访问特定行的内容(里面有一个字符串)


我原以为
推特['text',0]
推特.text[0]
会有用,但如果你正在寻找一种你可以使用的特殊钉住,情况就不是这样了。其工作原理如下:

获取数据

import pandas as pd
from io import StringIO

data = """
id tweet
12  "this is the first tweet"
34  "this is the second tweet"
48  "this is the third tweet"
59  "finally the fourth tweet"
"""

df = pd.read_csv(StringIO(data), delimiter='\s+')
使用
str.contains

first = df['tweet'].str.contains('first')
this = df['tweet'].str.contains('this')
fin = df['tweet'].str.contains('finally')
这将导致:

0     True
1    False
2    False
3    False
Name: tweet, dtype: bool 0     True
1     True
2     True
3    False
Name: tweet, dtype: bool 0    False
1    False
2    False
3     True
Name: tweet, dtype: bool

抱歉,您正在尝试执行
tweets['text']=tweets['text'].map(tweets_data)
?也可以使用tweets.ix[0]也许我不太明白,我只是想访问列文本中的行号X。您是否只对索引感兴趣,还是对列的内容感兴趣?我只对列的内容感兴趣,但是我认为
对于xrange中的x(0,len(tweets_data)):print tweets_data[x]['text']
会很好地工作。对于循环,一般来说,运行速度比使用pandas中内置的API工具慢。是的,一定是这样,但是循环返回我
UnicodeEncodeError:“ascii”编解码器无法对位置27处的字符u'\u2019'进行编码:序号不在范围内(128)
似乎我无法访问
tweets\u数据[0]['text']
edt:我只是添加到used.encode
0     True
1    False
2    False
3    False
Name: tweet, dtype: bool 0     True
1     True
2     True
3    False
Name: tweet, dtype: bool 0    False
1    False
2    False
3     True
Name: tweet, dtype: bool