Drop Multiple Columns Pandas Code Example

Snippet 1 yourdf.drop([‘columnheading1’, ‘columnheading2′], axis=1, inplace=True) Snippet 2 # Let df be a dataframe # Let new_df be a dataframe after dropping a column new_df = df.drop(labels=’column_name’, axis=1) # Or if you don’t want to change the name of the dataframe df = df.drop(labels=’column_name’, axis=1) # Or to remove several columns df = df.drop([‘list_of_column_names’], axis=1)… Continue reading Drop Multiple Columns Pandas Code Example

For Row In Column Pandas Code Example

Snippet 1 # Option 1 for row in df.iterrows(): print row.loc[0,’A’] print row.A print row.index() # Option 2 for i in range(len(df)) : print(df.iloc[i, 0], df.iloc[i, 2]) Snippet 2 df = pd.DataFrame([{‘c1’:10, ‘c2’:100}, {‘c1′:11,’c2’:110}, {‘c1′:12,’c2’:120}]) for index, row in df.iterrows(): print(row[‘c1’], row[‘c2’]) Copyright © Code Fetcher 2020    

Getting Dummies And Input Them To Pandas Dataframe Code Example

Snippet 1 note: dummies = pd.get_dummies(df[[‘column_1’]], drop_first=True) df = pd.concat([df.drop([‘column_1’],axis=1), dummies],axis=1) note:for more that one coloum keep ading in the list dummies = pd.get_dummies(df[[‘column_1’, ‘column_2′,’column_3’]], drop_first=True) df = pd.concat([df.drop([‘column_1’, ‘column_1’],axis=1), dummies],axis=1) Snippet 2 >>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 Similar… Continue reading Getting Dummies And Input Them To Pandas Dataframe Code Example

Group By Pandas Examples Code Example

Snippet 1 # Groups the DataFrame using the specified columns df.groupBy().avg().collect() # [Row(avg(age)=3.5)] sorted(df.groupBy(‘name’).agg({‘age’: ‘mean’}).collect()) # [Row(name=’Alice’, avg(age)=2.0), Row(name=’Bob’, avg(age)=5.0)] sorted(df.groupBy(df.name).avg().collect()) # [Row(name=’Alice’, avg(age)=2.0), Row(name=’Bob’, avg(age)=5.0)] sorted(df.groupBy([‘name’, df.age]).count().collect()) # [Row(name=’Alice’, age=2, count=1), Row(name=’Bob’, age=5, count=1)] Snippet 2 >>> n_by_state = df.groupby(“state”)[“state”].count() >>> n_by_state.head(10) state AK 16 AL 206 AR 117 AS 2 AZ 48 CA… Continue reading Group By Pandas Examples Code Example

How To Change Column Name In Pandas Code Example

Snippet 1 df = df.rename(columns = {‘myvar’:’myvar_new’}) Snippet 2 df.rename(columns={‘oldName1’: ‘newName1’, ‘oldName2’: ‘newName2′}, inplace=True, errors=’raise’) # Make sure you set inplace to True if you want the change # to be applied to the dataframe Snippet 3 df.rename(columns={“old_col1”: “new_col1”, “old_col2”: “new_col2”}) Snippet 4 print(df.rename(columns={‘A’: ‘a’, ‘C’: ‘c’})) # a B c # ONE 11 12… Continue reading How To Change Column Name In Pandas Code Example

Add Age Categories Pandas Dataframe Code Example

Snippet 1 X_train_data = pd.DataFrame({‘Age’:[0,2,4,13,35,-1,54]}) bins= [0,2,4,13,20,110] labels = [‘Infant’,’Toddler’,’Kid’,’Teen’,’Adult’] X_train_data[‘AgeGroup’] = pd.cut(X_train_data[‘Age’], bins=bins, labels=labels, right=False) print (X_train_data) Age AgeGroup 0 0 Infant 1 2 Toddler 2 4 Kid 3 13 Teen 4 35 Adult 5 -1 NaN 6 54 Adult Copyright © Code Fetcher 2020    

Add An Index Column Pandas Code Example

Snippet 1 df.reset_index(level=0, inplace=True) Snippet 2 df = df.set_index(‘col’) Snippet 3 import numpy as np import pandas as pd df = pd.DataFrame({‘month’: [2, 5, 8, 10], ‘year’: [2017, 2019, 2018, 2019], ‘sale’: [60, 45, 90, 36]}) df.set_index(‘month’) Similar Snippets Add An Index Column Pandas Code Example – pandas Index Max Pandas Code Example – pandas… Continue reading Add An Index Column Pandas Code Example

Append New Data On Pandas Dataframe Code Example

Snippet 1 df = df.append({‘index1’: value1, ‘index2’:value2,…}, ignore_index=True) Snippet 2 DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None) Snippet 3 df.loc[-1] = [2, 3, 4] # adding a row df.index = df.index + 1 # shifting index df = df.sort_index() # sorting by index # And you get: # A B C # 0 2 3 4 # 1… Continue reading Append New Data On Pandas Dataframe Code Example