Exploring data Flashcards
Learning how to explore data after you clean it.
Visualize how to get the variance of a dataframe using groupby
df.groupby(by=”col1”)[[“col2”,”col3”,”col4”]].var()
Visualize how to use .describe() on groups to get measurements by the percentiles parameter.
df.groupby(by=”col1”)[[“col2”,”col3”,”col4”]].describe(percentiles=[0.25,0.5,0.75])
What is a Histogram? Visualize how to create.
df.plot(kind=”hist”)
It displays the distribution of numerical data
It divides data into bins and shows frequency of observations in each bin
Pandas: Visualize how to create a bar chart
df.plot(kind=”bar”)
It compares different categories and shows values as bars of various lengths.
matplotlib: Visualize how to create a pie chart
labels = “L1”, “L2”, “L3”
sizes = [10,20,25]
fig, ax = plt.subplots()
ax.pie(sizes, labels=labels, autopct=’%1.1f%%’, pctdistance=1.25, labeldistance=.6, colors=[“C1”,”C2”,C3”])
Use pctdistance and labeldistance if you want the percentages outside of the pct.
A. Visualize how you can use .agg() on all columns
B. visualize how you can use .agg() on specific columns
C. Visualize how you can use .agg() using .groupby()
D. Visualize how to rename columns with .aagg
A. df.agg([‘mean’, ‘sum’, ‘max’])
B. df.agg({ ‘col1’: ‘mean’, ‘col2’: [‘sum’, ‘min’], ‘col3’ : lambda x: x.std()})
C. df.groupby(‘col_group’).agg({‘col1’: ‘mean’, ‘col2’: ‘sum’, ‘col3’ : ‘max’})
D. df.groupby(‘group_column’).agg(mean_col1=(‘col1’, ‘mean’), sum_col2=(‘col2’, ‘sum’)
Visualize how to reset the index of a DataFrame
df.reset_index()
Visualize an example of how to use groupby to calculate mean
data = { ‘model’: [‘Car A’, ‘Car A’, ‘Car B’, ‘Car B’, ‘Car C’], ‘city_mpg’: [20, 22, 25, 27, 18]}
df = pd.DataFrame(data)
mean_mpg = df.groupby(‘model’)[‘city_mpg’].mean()
print(mean_mpg)
Output -
model city_mpg
Car A 21.0,
Car B 26.0 ,
Car C 18.0
A. Visualize how to calculate the mean on a dataframe.
B. Visualize how to calculate the mean on a column
A. df.mean(numeric_only=True)
B. df.groupby(“col”).mean(numeric_only=True)
What does standard deviation measure?
How much each point differs from the mean, or how spread out the data is.
.std()
What is variance?
Variance helps us understand how the numbers in a group differ from the average, giving a sense of how scattered or clustered the data is.
.var()
What are quantiles? And how do you use them?
Quantiles are values that split a group of data into equal parts.
df[[‘col1’,’col2’,’col3’]].qunatile(q=[.25,.50,.75,1])
You can change the percentages to be whatever you want.
What method would you use to show capital gains and capital loss?
dataframe[[“capital-gain”, “capital-loss”]].sum()
What are the different panda plotting methods?
A. df.hist(figsize=(#,#)); or df[col].hist(figsize=(8,8));
B. df.plot(kind=”box”, figsize=(#,#)) or df[col].plot(kind=”box”, figsize=(#,#))
C. df.bar() or df[col].bar()
D. df.pie() or df[col].pie()
E. pd.plotting.scatter_matrix()
F. df.scatter() or df[col].scatter()
G. df.box() or df[col].box()
How would you plot a bar chart with the value counts?
df[‘col’].value_counts().plot(kind=’bar’);
Using the df.plot(kind=’scatter’), visualize how to specify what axis each column will be plotted.
df.plot(x=’col1’, y=’col2’, kind=’scatter’);
A. Visualize how to use .hist() return a matplotlib subplot, change its transparency, and change the figure size
B. Visualize how to layer a new histogram using the same subplot that was returned.
A. ax = df_A[‘col1’].hist(alpha=0.5, figsize=(#,#,), label = ‘title1’);
B. df_B[‘col1’].hist(alpha=0.5, figsize=(#,#,), label=’title2’, ax=ax);
C.
ax.set_title(‘TITLE’);
ax.set_xlabel(‘X-AXIS TITLE’);
ax.set_ylabel(‘Y-AXIS TiTLE’);
ax.legend(loc=’upper right’);
If you had a column containing the sales for each week for an entity, how would you find the row corresponding to the minimum sales or the worst week?
.idxmin()
ex:
#Step 1: find the index of the minimum sales
worst_week_index = df[‘col’].idxmin()
#Step 2: Access the corresponding week
worst_week = df.loc[worst_week_index, ‘week’]
#Print
print(f”The worst week is: {worst_week}”}
A. Visualize how to customize the x-axis and the y-axis of your Pandas plots.
B. Visualize how to set the minimum and maximum values of your Pandas plot
A.
df.plot(xlabel=’X Axis Label’)
df.plot(ylabel=’Y Axis Label’)
B.
df.plot(ylim=(min_value,max_value))
Visualize how to set the color for your Pandas plot
df.plot(color=’color_name’)
You can use color names or hexadecimal color codes.
Visualize how to set the legend for your Pandas plot
df.plot(legend=True)
How can you use .index with .value_counts()?
You can use .index with .value_counts to access the unique values in a column along with their counts
EX.
# Example DataFrame
data = {‘fruits’: [‘apple’, ‘banana’, ‘apple’, ‘orange’, ‘banana’, ‘apple’]}
df = pd.DataFrame(data)
# Calculate value counts and index
counts_index = df[‘fruits’].value_counts().index
#Print
print(counts_index)
#Output
Index([‘apple’, ‘banana’, ‘orange’], dtype=’object’)
What does the .index do?
The .index attribute gives you the unique values (the categories)
What does .values do?
The .values attribute returns a NumPy array including duplicates, in order of appearance.
This is useful for nummerical computations or array manipulations or for converting to Python lists such as df[‘col’].values.tolist()