2015-11-12 50 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

相關問題