Pandas Flashcards
How do you import pandas?
import pandas as pd
How do you load a CSV file into a dataframe?
df = pd.read_csv('data.csv')
How do you load a JSON file into a dataframe?
df = pd.read_json('data.json')
How do you view the first five rows of a dataframe?
df.head()
How do you view the last five rows of a dataframe?
df.tail()
How do you view information about the data?
df.info()
How do you return a new dataframe with no empty cells?
df_new = df.dropna()
How do you return the same dataframe with no empty cells?
df.dropna(inplace = True)
How do you replace empty cells with a value in the entire dataframe?
df.fillna("value", inplace = True)
How do you replace empty cells in specified column(s) with a value?
df["column"].fillna("value", inplace = True)
How do you calculate the MEAN of a column?
x = df["column"].mean()
How do you calculate the MEDIAN of a column?
x = df["column"].median()
How do you calculate the MODE of a column?
x = df["column"].mode()[0]
How do you convert a column to datetime?
df["date"] = pd.to_datetime(df["date"])
How do you remove NULL rows using specific column(s) as a reference?
df.dropna(subset=["column"...], inplace = True)
How do you replace a specific cell value?
df.loc[idx, "column"] = "new_value"
How do you loop through rows?
for x in df.index: print(x)
How do you drop a specific row?
df.drop(idx, inplace = True)
How do you view duplicate rows?
df.duplicated()
How do you drop duplicated rows?
df.drop_duplicates(inplace = True)
How do you drop column(s)?
df.drop(columns=["column", ...], inplace=True)
How do you convert a column to a different data type?
df["col"] = df["col"].astype(type)
Available types: “int”, “float”, “str”, “bool”, “datetime64”