如何使用Pandas通过多个字符串变量过滤行

时间:2019-06-29 09:58:44

标签: python pandas

我正在学习Pandas,并希望通过字符串变量列表来过滤数据集中的行。

我用一种方法对变量进行排序。这很好。代码如下:

import plotly as py
import plotly.graph_objs as go
import pandas as pd

py.offline.init_notebook_mode(connected=True)

df = pd.read_csv('C:/Users/Documents/Python/CKANMay.csv')
sd = df.nlargest(3,'Views')
fd = sd.sort_values(by='Views', ascending = True)


my_data = [go.Bar( x = fd.Views, y = fd.Publisher, orientation = 'h',)]
my_layout = go.Layout({"title": "Most popular publishers",
                       "yaxis": {"title":"Publisher"},
                       "xaxis": {"title":"Views"},
                       "showlegend": False}, yaxis=go.layout.YAxis(
        tickmode='array',
        automargin=True,
    )
    )


fig = go.Figure(data = my_data, layout = my_layout)

py.offline.iplot(fig)

我想使用针对特定字符串变量的过滤器重新创建此代码。下面是我的返回语法错误的代码:

import plotly as py
import plotly.graph_objs as go
import pandas as pd

py.offline.init_notebook_mode(connected=True)

df = pd.read_csv('C:/Users/Documents/Python/CKANappended.csv')
df1 = df.sort_values(by='Views', ascending = True)
df2 = df1[df1.Publisher.isin(['Department of Finance, Services and Innovation', 'Office of Environment and Heritage (OEH)', 
                              'Department of Planning and Environment (DPE)'])

my_data = [go.Bar( x = df2.Views, y = df2.Publisher, orientation = 'h',)]
my_layout = go.Layout({"title": "Most popular publishers",
                       "yaxis": {"title":"Publisher"},
                       "xaxis": {"title":"Views"},
                       "showlegend": False}, yaxis=go.layout.YAxis(
        tickmode='array',
        automargin=True,
    )
    )


fig = go.Figure(data = my_data, layout = my_layout)

This is the syntax error I get

这个语法错误对我来说没有意义,因为代码中的唯一区别是Pandas属性。

我的预期输出与包含第一个代码集的图形相同。 Example here

非常感谢您的帮助。

0 个答案:

没有答案