목록Pandas (57)
Note
How to replace missing values of multiple numeric columns with the mean? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution df_out = df[['Min.Price', 'Max.Price']] = df[['Min.Price', 'Max.Price']].apply(lambda x: x.fillna(x.mean())) print(df_out.head()) # output Min.Price Max.Price 0 12.900000 18.800000 1 29.200000 38.700000 2 25.9000..
How to count the number of missing values in each column? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution n_missings_each_col = df.apply(lambda x: x.isnull().sum()) n_missings_each_col.argmax() # output 'Luggage.room'
How to check if a dataframe has any missing values? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution df.isnull().values.any()
How to rename a specific columns in a dataframe? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution # Step 1: df=df.rename(columns = {'Type':'CarType'}) # or df.columns.values[2] = "CarType" # Step 2: df.columns = df.columns.map(lambda x: x.replace('.', '_')) print(df.columns) # output Index(['Manufacturer', 'Model', 'CarType', 'Min_P..
How to extract the row and column number of a particular cell with given criterion? # Input df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv') # Solution # Get Manufacturer with highest price df.loc[df.Price == np.max(df.Price), ['Manufacturer', 'Model', 'Type']] # Get Row and Column number row, col = np.where(df.values == np.max(df.Price)) # Get the va..