Created
October 29, 2016 01:17
-
-
Save rosiecakes/86c2a6f758fcdc31e7ecedd1163c5c25 to your computer and use it in GitHub Desktop.
dataquest pandas intro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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