2017-04-06 166 views
0

我想選擇一個熊貓的數據幀中的專欄中,我正在讀選擇一個數據幀大熊貓蟒蛇的一列

tweets = pd.read_csv(r'C:\Users\PedroLuis\Documents\Manita\LASSO 20170219-20170402.csv', sep = " , ", engine='python') 
tweets = pd.DataFrame(tweets) 

當我列出我所看到的列是

list(tweets) 
Out: ['"","text","favorited","favoriteCount","replyToSN","created","truncated","replyToSID","id","replyToUID","statusSource","screenName","retweetCount","isRetweet","retweeted","longitude","latitude"'] 

我試圖通過它的名稱來選擇第二列>

tweets['text'] 

而且我得到這個錯誤:

KeyError: 'text'

+0

這很奇怪。當你嘗試'tweets.iloc [:,1]'時會發生什麼? PS。你不需要'tweets = pd.DataFrame(tweets)'線,因爲read_csv()已經返回一個數據幀 –

+0

tweets.columns的輸出是什麼? –

回答

2

。在你的月=「」,它使所有的列相結合的空間。

將其更改爲

tweets = pd.read_csv(r'C:\Users\PedroLuis\Documents\Manita\LASSO 20170219-20170402.csv', sep = ",", engine='python') 

你應該能夠調用鳴叫[「文本」]

1

如果在列表()的輸出仔細觀察,你會發現一個包含整個字符串用單引號括起來,每個頭用雙引號括起來,這意味着大熊貓沒有像你期待的那樣解釋這一行。

Out: ['"","text","favorited","favoriteCount","replyToSN","created","truncated","replyToSID","id","replyToUID","statusSource","screenName","retweetCount","isRetweet","retweeted","longitude","latitude"'] 

儘管它看起來應該

Out: ['','text','favorited','favoriteCount','replyToSN','created','truncated','replyToSID','id','replyToUID','statusSource','screenName','retweetCount','isRetweet','retweeted','longitude','latitude'] 

我不知道你的輸入是什麼樣子,但是,Niche.P說,清理你的分離器參數可能是一個解決方案。否則它可能是encoding issue