开发者社区> 问答> 正文

pd.DataFrame.from_dict()没有给出预期的结果

我想得到这个维基百科数据集(people_wiki.csv)中每个单词的单词计数。我能够得到每个单词并将其作为字典出现,但我无法将字典键值对拆分为单独的列。我已经尝试了几种方法(from_dict,from_records,to_frame,pivot_table等)这在python中是可行的。

Samle数据集:

URI name text

http://dbpedia.org/resource/George_Clooney George Clooney 'george timothy clooney born may 6 1961 is an american actor writer producer director and activist he has received three golden globe awards for his work as an actor and two academy awards one for acting and the other for producingclooney made his...'
我试过了:

clooney_word_count_table = pd.DataFrame.from_dict(clooney['word_count'], orient='index', columns=['word','count']
我也尝试过:

clooney['word_count'].to_frame()
这是我的代码:

people = pd.read_csv("people_wiki.csv")
clooney = people[people['name'] == 'George Clooney']

from collections import Counter
clooney['word_count']= clooney['text'].apply(lambda x: Counter(x.split(' ')))

clooney_word_count_table = pd.DataFrame.from_dict(clooney['word_count'], orient='index', columns=['word','count']
clooney _word_count_table
输出:

   word_count

35817 {'george': 1, 'timothy': 1, 'clooney': 9, 'ii': ...
我希望从clooney_word_count_table获得一个包含2列的输出数据帧:

word count
normalize 1
george 3
combat 1
producer 2

展开
收起
一码平川MACHEL 2019-01-22 17:21:19 4809 0
1 条回答
写回答
取消 提交回答
  • 问题是,clooney是(含一排索引35817)一个数据帧,所以clooney['word_count']是一个系列的指数35817包含一个值(您计数字典)。

    DataFrame.from_dict然后将这个系列视为等同于{35817: {'george': 1,...}哪个系统会给你带来令人困惑的结果。

    尝试类似的东西:

    c = Counter()
    cloony['text'].apply(lambda x: c.update(x.split()))
    pd.from_dict(c, orient='index', columns=['count'])

    2019-07-17 23:26:22
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载