How To Get Dummies In A Dataframe Pandas Code Example

Snippet 1 note: dummies = pd.get_dummies(df[[‘column_1’]], drop_first=True) 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) Snippet 2 >>> s = pd.Series(list(‘abca’)) >>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 Snippet 3 df = pd.get_dummies(df, columns=[‘type’])… Continue reading How To Get Dummies In A Dataframe Pandas Code Example

Create Additional Rows For Missing Dates Pandas Code Example

Snippet 1 In [11]: idx = pd.period_range(min(df.date), max(df.date)) …: results.reindex(idx, fill_value=0) …: Out[11]: f1 f2 f3 f4 2000-01-01 2.049157 1.962635 2.756154 2.224751 2000-01-02 2.675899 2.587217 1.540823 1.606150 2000-01-03 0.000000 0.000000 0.000000 0.000000 2000-01-04 0.000000 0.000000 0.000000 0.000000 2000-01-05 0.000000 0.000000 0.000000 0.000000 2000-01-06 0.000000 0.000000 0.000000 0.000000 2000-01-07 0.000000 0.000000 0.000000 0.000000 2000-01-08 0.000000 0.000000… Continue reading Create Additional Rows For Missing Dates Pandas Code Example

Delete Columns Pandas Code Example

Snippet 1 df.drop(columns=[‘B’, ‘C’]) Snippet 2 df.drop([‘feature_1’,’feature_2’], inplace = True, axis = 1) Snippet 3 df = df.drop([‘B’, ‘C’], axis=1) Snippet 4 df = pd.DataFrame(np.arange(12).reshape(3, 4), … columns=[‘A’, ‘B’, ‘C’, ‘D’]) >>> df A B C D 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 Drop columns df.drop([‘B’,… Continue reading Delete Columns 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