3rd Flashcards
move a column to become the index
df.set_index(‘c’)
Reset index
df.reset_index()
Remove index col
df.reset_index(drop = True)
Multiple index columns
df.set_index([‘c’, ‘c1’])
Select only year from date
df[‘c’].dt.year
Select only month from date
df[‘c’].dt.month
Detect missing values
df.isna()
name breed 0 false false 1 false True 2 false True 3 false False
Does df columns have NA
df.isna().any()
name false
breed True
color True
Count # of na in a column
df.isna().sum()
name 0
breed 2
color 3
Remove all NAs from a df
df.dropna()
Replace NAs with a missing value
df.fillna(0)
List of dictionaries
list_of_dicts = [
{‘name’: ‘Ginger’, ‘Breed : ‘Lab’, ‘kg’:22},
{‘name’: ‘qwuire’, ‘Breed : ‘good’, ‘kg’:12}
]
name breed kg 0 Ginger Lab 22 1 qwuire good 12
Dictionary of lists
dict_of_lists = {
‘name’ : [‘Ginger’, ‘qwuire’],
‘breed’ : [‘Lab’, ‘good’],
‘weight’ : [22, 12]
}
name breed kg 0 Ginger Lab 22 1 qwuire good 12
Write csv
pd.to_csv(‘file/path’)
Inner join
Only return rows where the values match in both tables
new_df = df1.merge(df2, on = ‘col’)