Add columns
# specify the name of the new column -> dataframe["new_columns"]
# Add a column named "name_lengths" that includes the length of each name
babynames["name_lengths"] = babynames["Names"].str.len()
babynames.head(5)
Sort by the temporary column
# Sort by the temporary column
babynames = babynames.sort_values(by = "name_lengths", ascending=False)
babynames.head()
.map
# First, define a function to count the number of times "dr" or "ea" appear in each name
def dr_ea_count(string):
return string.count("dr") + string.count("ea")
# Then, use 'map' to apply 'dr_ea_count' to each name in the "Name" column
babynames["dr_ea_count"] = babynames["Name"].map(dr_ea_count)
# Sort tje DataFrame by the new "dr_ea_count" column so we can see our handwork
babynames.sort_values(by = "dr_ea_count", ascending = False).head(5)
.drop
# Drop our "dr_ea_count" and "length" columns from the DataFrame
babynames = babynames.drop(["dr_ea_count", "name_lengths"], axis="columns")
babynames.head(5)
※ Notice: Important point: pandas table operations do not occure in-place. Calling dataframe.drop(...) will output a copy of dataframe with the row/column of interest removed, without modifying the original dataframe table.
'Computer Science 🌋 > Machine Learning🐼' 카테고리의 다른 글
Aggregation in Pandas (0) | 2023.05.23 |
---|---|
Aggregating Data with GroupBy in Pandas (0) | 2023.05.23 |
Handy Utility Functions in Pandas (0) | 2023.05.23 |
Conditional Selection in Pandas (0) | 2023.05.23 |
Indexing in Pandas (0) | 2023.05.22 |