carl
carl

Reputation: 77

Use 'loc' to select multiple columns

I have a data set of news with 3 columns: lead_paragraph, _id, web_url.

The following code returns a row matching the id along with News lead_paragraph.

ds = pd.read_csv("nytimes.csv")
def item(id):
    return ds.loc[ds['_id'] == id]['lead_paragraph'].tolist()[0]

How to get web_url also with lead_paragraph in the list?

Got an error after trying one solution

return ds.loc[ds['_id'] == id], ['web_url', 'lead_paragraph']].tolist()
                                                             ^
SyntaxError: invalid syntax

Upvotes: 2

Views: 6116

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Use df.loc[row indexer, columns indexer],

df.loc[df['_id'] == id, ['web_url', 'lead_paragraph']].values[0].tolist()

Where row indexer is a boolean series and column indexer is a list of column labels.

Upvotes: 2

Related Questions