2015-11-12 49 views
1

我使用Pandas创建了3列DataFrame,我只是试图访问特定行的内容(里面有一个字符串)。如何访问DataFrame中的特定行

tweets = pd.DataFrame() 
tweets['text'] = map(lambda tweet: tweet['text'], tweets_data) 
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data) 
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data) 

我认为tweets['text',0]tweets.text[0]会工作,但它并非如此

+0

对不起你试图做'鸣叫['text'] =推文['t分机']。图(tweets_data)'? – EdChum

+2

也可以使用tweets.ix [0] – reptilicus

回答

0

如果你正在寻找可以使用str.contains特定的刺痛。它的工作原理是这样的:

获取数据

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 
+0

我不确定要理解,我只是想访问列的文本中的行号x – kwn

+0

您只对索引感兴趣,或者对内容感兴趣的列? – Leb

+0

我只对列的内容感兴趣,但我认为'对于x的xrange(0,len(tweets_data)): \t print tweets_data [x] ['text']'会工作得很好 – kwn

相关问题