简体   繁体   English

选择数据框熊猫python的一列

[英]select one column of a data frame pandas python

I am trying to select a column from a pandas data frame I am reading 我正在尝试从正在读取的熊猫数据框中选择一列

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

When I list the columns what I see is 当我列出列时,我看到的是

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

I try to select the second column by its name> 我尝试通过其名称选择第二列

tweets['text']

And I get this error: 我得到这个错误:

KeyError: 'text' KeyError:“文本”

There is a space in your sep = " , " which causes all the column to combine. sep =“,”中有一个空格,导致所有列合并。

Change it to 更改为

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

You should be able to call tweet['text'] 您应该可以调用tweet ['text']

If you look carefully at the output of list(), you'll notice one entire string contained in single quotes, and each header surrounded in double-quotes, which means pandas hasn't interpreted the line as you are expecting. 如果仔细查看list()的输出,您会注意到一个完整的字符串包含在单引号中,并且每个标头都用双引号引起来,这意味着pandas并未按照您的期望来解释该行。

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

Whereas it should look like 看起来应该像

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

I don't know what your input looks like, but, as Niche.P says, cleaning up your separator parameter could be a solution. 我不知道您的输入是什么样子,但正如Niche.P所说,清理分隔符参数可能是一个解决方案。 Otherwise it could be an encoding issue . 否则可能是编码问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM