Python Pandas Flashcards
What are pandas in Python?
A data analysis and manipulation library.
How do you import pandas in Python?
import pandas as pd
What is a DataFrame in pandas?
A 2D labeled data structure with columns of potentially different types.
What is a Series in pandas?
A 1D labeled array capable of holding any data type.
How do you create a DataFrame from a dictionary?
pd.DataFrame({‘col1’: [1
How do you read a CSV file with pandas?
pd.read_csv(‘file.csv’)
How do you write a DataFrame to a CSV file?
df.to_csv(‘file.csv’
How do you display the first 5 rows of a DataFrame?
df.head()
How do you display the last 5 rows of a DataFrame?
df.tail()
How do you get DataFrame column names?
df.columns
How do you get DataFrame index values?
df.index
How do you get a quick summary of a DataFrame?
df.info()
How do you get statistical summary of numeric columns?
df.describe()
How do you select a single column from a DataFrame?
df[‘column_name’]
How do you select multiple columns from a DataFrame?
df[[‘col1’
How do you select rows by index?
df.loc[0] or df.iloc[0]
How do you filter rows by condition?
df[df[‘col’] > 10]
How do you add a new column to a DataFrame?
df[‘new_col’] = values
How do you drop a column from a DataFrame?
df.drop(‘col’
How do you drop a row from a DataFrame?
df.drop(index
How do you rename columns in a DataFrame?
df.rename(columns={‘old’: ‘new’}
How do you check for missing values?
df.isnull()
How do you fill missing values with 0?
df.fillna(0)
How do you drop rows with missing values?
df.dropna()
How do you sort a DataFrame by column?
df.sort_values(‘col’)
How do you reset the index of a DataFrame?
df.reset_index(drop=True)
How do you set a column as the index?
df.set_index(‘col’
How do you group data by a column?
df.groupby(‘col’)
How do you aggregate grouped data?
df.groupby(‘col’).agg({‘val’: ‘sum’})
How do you merge two DataFrames?
pd.merge(df1
How do you concatenate two DataFrames vertically?
pd.concat([df1
How do you concatenate two DataFrames horizontally?
pd.concat([df1
How do you pivot a DataFrame?
df.pivot(index=’a’
How do you melt a DataFrame?
pd.melt(df
How do you check for duplicates?
df.duplicated()
How do you drop duplicates?
df.drop_duplicates()
How do you convert column to datetime?
pd.to_datetime(df[‘col’])
How do you extract year from datetime column?
df[‘col’].dt.year
How do you apply a function to a column?
df[‘col’].apply(func)
How do you map values in a column?
df[‘col’].map({‘old’: ‘new’})
How do you convert a column to numeric?
pd.to_numeric(df[‘col’]
How do you create a DataFrame from a list?
pd.DataFrame(data
How do you export a DataFrame to Excel?
df.to_excel(‘file.xlsx’
How do you read an Excel file?
pd.read_excel(‘file.xlsx’)
How do you check data types of columns?
df.dtypes
How do you change data type of a column?
df[‘col’] = df[‘col’].astype(‘int’)
How do you get the number of rows and columns?
df.shape
How do you count unique values in a column?
df[‘col’].nunique()
How do you get value counts in a column?
df[‘col’].value_counts()
How do you sample random rows from a DataFrame?
df.sample(n=5)
How do you get the memory usage of a DataFrame?
df.memory_usage()
How to read a CSV file?
employee_df = pd.read_csv(‘employee_information.csv’)
How to import the pandas lib to Python?
Import pandas as pd
What is the key structure of pandas?
df (data frame)
How to create a data frame?
x = pd.df({Employe ID:[1,2,3,4]’})
How do I get a statistical summary?
x.describe() (you will get the mean,std,min,25%, 50%, 75%,max)
How to Normalize the data?
df[‘normalized’] = (df[‘col’] - df[‘col’].min()) / (df[‘col’].max() - df[‘col’].min())
What is MatLibplot?
What is sklearn?