Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- # Check for all elements in the column "origin" whether they are equal to "europe"
- mask1 = cars.origin == 'europe'
- mask1.all()
- mask2 = df.Country == 'ARG'
- mask2.any()
- #########################################################
- df.loc[:,'Order_ID']
- mask1 = df.Sport == "Volleyball"
- df.loc[mask1]
- #########################################################
- mask1 = df['genres'].str.contains('Science Fiction')
- mask2 = df['genres'].str.contains('Action')
- mask3 = df['cast'].str.contains('Bruce Willis')
- df.loc[mask1 & mask2 & mask3, ['title', 'genres', 'cast', 'vote_average']].sort_values(by=['vote_average'],ascending = False)
- ###########################################################
- # Filter by Date
- mask1 = df['release_date'].dt.date.astype(str) >= '2010'
- mask2 = df['release_date'].dt.date.astype(str) <= '2015'
- mask3 = df['production_companies'].str.contains('Pixar')
- df.loc[mask1 & mask2 & mask3].sort_values(by=['revenue_musd'], ascending = False)
- mask4 = df.release_date.between(1960, 1969, inclusive = 'both')
- df.loc[mask4]
- mask5 = df.release_date.isin([1960,1961,1962,1964]) # isin
- df.loc[mask5]
- mask6 = df.release_date >= 1992
- df.loc[mask6]
- df.loc[~mask6]
- ##########################################################
- # Filter by many conditions
- mask1 = df['genres'].str.contains('Action')
- mask2 = df['genres'].str.contains('Thriller')
- mask3 = df['spoken_languages'].str.contains('English')
- df.loc[df['vote_average'] >= 7.5].loc[(mask1 | mask2) & mask3].sort_values(by=['release_date'], ascending = False)
- mask1 = df.Country.isin(["ITA", "FRA", "ESP", "USA"])
- df.loc[mask1]
- ###############################################################
- mask1 = titanic.sex == male
- mask2 = titanic.dtypes == object
- titanic.loc[:, ~mask2] # gen only non object values (numeric)
- titanic.loc[mask1,~maks2] # only males with numeric data
- male_survived = titanic.loc[mask1 & mask2, :]
- titanic.loc[mask1 | mask2, ['sex','survived']]
- ###########################################################
- df_auto = df_auto.loc[df_auto.Kilometes > 1111]
- df_auto = df_auto.loc[df_auto['Years Automobile'] > 0)]
- titanic.loc[titanic.sex=="Male", Age] # get only Age column
- ###########################################################
- index_babies = titanic.loc[titanic.age < 1, 'age'].index
- titanic.loc[titanic.age < 1, 'age'] = 1 # where age < 1 changed the value to 1
- titanic.loc[index_babies] # how to check the result
- ############################################################
- not_73_74 = cars.loc[~cars.model_year.isin ([73,74]), ['mpg', 'name']] # not
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement