【发布时间】:2015-06-17 13:40:48
【问题描述】:
我想计算列中的热门主题。有些字段有逗号或点我想用它们创建一个新行。
import pandas as pd
from pandas import DataFrame, Series
sbj = DataFrame(["Africa, Business", "Oceania",
"Business.Biology.Pharmacology.Therapeutics",
"French Litterature, Philosophy, Arts", "Biology,Business", ""
])
sbj
我想将任何包含“.”的字段拆分为一个新的字段。或“。”
sbj_top = sbj[0].apply(lambda x: pd.value_counts(x.split(",")) if not pd.isnull(x) else pd.value_counts('---'.split(","))).sum(axis = 0)
sbj_top
我在尝试重新拆分('.')时遇到错误(AttributeError)
sbj_top = sbj_top.apply(lambda x: pd.value_counts(x.split(".")) if not pd.isnull(x) else pd.value_counts('---'.split(","))).sum(axis = 0)
sbj_top
我想要的输出
sbj_top.sort(ascending=False)
plt.title("Distribution of the top 10 subjects")
plt.ylabel("Frequency")
sbj_top.head(10).plot(kind='bar', color="#348ABD")
【问题讨论】:
-
你想要的输出是什么?
-
快速查看第一次拆分的输出会显示结果中的空格。您需要使用 trim()。
-
您要保留原始索引吗?例如,第一行应该变成“0, Africa”和“0, Business”吗?
-
嗨@cphlewis 我只想做
value_counts()这样我就可以做一个.plot()
标签: python-2.7 pandas ipython-notebook