Skip to content

Instantly share code, notes, and snippets.

@rosiecakes
Created October 29, 2016 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosiecakes/86c2a6f758fcdc31e7ecedd1163c5c25 to your computer and use it in GitHub Desktop.
Save rosiecakes/86c2a6f758fcdc31e7ecedd1163c5c25 to your computer and use it in GitHub Desktop.
dataquest pandas intro
column_list = food_info.columns.tolist()
gram_columns = [col for col in column_list if col.endswith('(g)')]
gram_df = food_info[gram_columns]
gram_df.head(3)
Use the columns attribute to return the column names in food_info and convert to a list by calling the method tolist()
Create a new list, gram_columns, containing only the column names that end in "(g)". The string method endswith() returns True if the string object calling the method ends with the string passed into the parentheses.
Pass gram_columns into bracket notation to select just those columns and assign the resulting DataFrame to gram_df
Then use the DataFrame method head() to display the first 3 rows of gram_df.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment