我有一个通过txt文件读到的pandas dataframe
:
training = pd.read_csv('training_data.txt')
这些是列:
>> print training.columns.values`
['segment' 'cookie_id' 'num_visits' 'num_page_views']
我对按行显示num_page_views
密度的图表感兴趣,如下所示:
plot = ggplot(training, aes(x='num_visits')) + geom_density()
+ xlim(0,20) + facet_wrap( ~ 'segment')
print plot
它提供以下错误:
TypeError Traceback(最近一次调用 最后)in() 1 ----> 2 plot = ggplot(training,aes(x =' num_visits'))+ geom_density()+ xlim(0,20)+ facet_wrap(〜' segment') 3打印图
TypeError:一元〜:' str'
的坏操作数类型
答案 0 :(得分:2)
在yhat ggplot中,facet选项不带“〜”。你可以在documentation中找到它(为方便起见,这里复制):
import pandas as pd
meat_lng = pd.melt(meat, id_vars=['date'])
p = ggplot(aes(x='date', y='value'), data=meat_lng)
p + geom_point() + \
stat_smooth(colour="red") + \
facet_wrap("variable")
p + geom_hist() + facet_wrap("color")
p = ggplot(diamonds, aes(x='price'))
p + geom_density() + \
facet_grid("cut", "clarity")
p = ggplot(diamonds, aes(x='carat', y='price'))
p + geom_point(alpha=0.25) + \
facet_grid("cut", "clarity")