Programming Part 2 Flashcards
(25 cards)
What is a DataFrame in pandas?
A two-dimensional labeled data structure similar to a table.
What is a Series in pandas?
A one-dimensional labeled array.
How do you read a CSV into a pandas DataFrame?
Using pd.read_csv(‘filename.csv’)
How do you inspect the first few rows of a DataFrame?
Using the .head() method.
How do you get column names from a DataFrame?
Using df.columns
How do you select a column from a DataFrame?
Using df[‘column_name’]
How do you select multiple columns?
Using df[[‘col1’, ‘col2’]]
What is .loc[] used for in pandas?
Label-based indexing for rows and columns.
What is .iloc[] used for in pandas?
Position-based indexing for rows and columns.
How do you filter rows based on a condition?
Using boolean indexing: df[df[‘column’] > value]
How do you check for missing values?
Using df.isnull()
How do you drop rows with missing values?
Using df.dropna()
How do you fill missing values?
Using df.fillna(value)
How do you rename columns in pandas?
Using df.rename(columns={‘old’: ‘new’})
How do you change the data type of a column?
Using df[‘col’] = df[‘col’].astype(new_type)
What does .groupby() do?
Groups rows based on column values for aggregation.
How do you calculate mean for each group?
df.groupby(‘col’).mean()
What is the purpose of .agg()?
To apply multiple aggregation functions at once.
How do you count unique values in a column?
Using df[‘col’].nunique()
What does df.value_counts() do?
Counts occurrences of unique values in a Series.
How do you concatenate two DataFrames?
Using pd.concat([df1, df2])
How do you merge two DataFrames?
Using pd.merge(df1, df2, on=’key’)
What is the difference between merge and join in pandas?
merge is a method; join is an attribute method for DataFrames.
How do you reset the index of a DataFrame?
Using df.reset_index()