Pandas Flashcards
(27 cards)
How do you import the pandas library with alias?
import pandas as pd
How do you create a DataFrame from a list of lists?
pd.DataFrame(data, columns=[‘col1’, ‘col2’])
How do you convert a column to float type in a DataFrame?
df[‘column’].astype(‘float’, copy=False)
How do you view the data types of all columns in a DataFrame?
df.dtypes
How do you create a DataFrame from a dictionary?
pd.DataFrame({‘col1’: […], ‘col2’: […]})
How do you create a DataFrame using zip()?
pd.DataFrame(list(zip(list1, list2)), columns=[‘col1’, ‘col2’])
How do you view the first 5 rows of a DataFrame?
df.head()
How do you view the last 5 rows of a DataFrame?
df.tail()
How do you select a single column from a DataFrame?
df[‘column’] or df.column
How do you filter rows where Age > 25?
df[df[‘Age’] > 25]
How do you filter with multiple conditions (Age > 25 and Grade == ‘A’)?
df[(df[‘Age’] > 25) & (df[‘Grade’] == ‘A’)]
How do you add a new column to a DataFrame with a scalar value?
df[‘new_column’] = value
How do you add a new column to a DataFrame with a list of values?
df[‘new_column’] = [val1, val2, …]
How do you rename the column of a Series?
series.name = ‘NewName’
How do you rename the index of a Series?
series.index.name = ‘IndexName’
How do you drop a column from a DataFrame?
df.drop(‘column’, axis=1)
How do you drop a row by index from a DataFrame?
df.drop(index_label)
How do you reset the index of a DataFrame?
df.reset_index(drop=True)
How do you sort a DataFrame by column values?
df.sort_values(‘column’)
How do you check for null (missing) values?
df.isnull()
How do you drop rows with null values?
df.dropna()
How do you fill null values with a default?
df.fillna(default_value)
How do you get the summary statistics of a DataFrame?
df.describe()