Practice day 12 of Learning Python for Data Science
Test your understanding of Python Data Structure, which we learned in our previous lesson of Day 12 of Learning Python for Data Science, with these targeted practice questions.
Welcome back to Day 12 of Learning Python for Data Science journey! In the last article, we explored:
✅ Local Scope
✅ Enclosing Scope
✅ Global Scope
✅ Built-in Scope
Now, it’s time to solve the practice questions given in the previous article.
Each question is followed by a detailed explanation and output.
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 28],
'City': ['New York', 'London', 'Paris']
}
# Create a DataFrame from the dictionary
df = pd.DataFrame(data)
# Print the DataFrame
print(df)
df.head()
df['Name']
df.isnull().sum()
df.sort_values(by='Status')
df[df['Patient_ID']>10]
df.rename(columns = {'Fraud_Flag':'Is_fraud'}, inplace=True)
df.drop(columns = 'Paid_Amount', inplace=True)
df.fillna(df['Billed_Amount'].mean(), inplace=True)
df['New_column'] = 0
loc
and iloc
.df.loc[1,] # printing 1 row all column using the index label.
df.iloc[1:2,:] # printing 1 row all columns using index position.
df['Provider_ID'].astype(float)
df.mean(numeric_only=True)
df['Provider_ID'].nunique()
groupby()
to find the mean of a column by category.df.groupby('Status')['Billed_Amount'].mean()
merge()
.df.merge(df2, on = 'Claim_ID')
df['Provider_ID'].mode()
df['Billed_Amount'].sum()
df['Paid_Amount'] = df['Paid_Amount'].apply(lambda x : x**2 )
pivot_table()
to summarize data.pd.pivot_table(df, index=['Provider_ID'], values= ['Billed_Amount','Paid_Amount'],columns=['Status'], aggfunc="sum",fill_value=0)
melt()
.df.melt(id_vars=['Claim_ID'], value_vars=['column_to_unpivot'])
applymap()
to apply a function to every element in the DataFrame.df.applymap(lambda x : x + 1 if isinstance(x, (int, float)) else x)
map()
to apply a function to a Series.df['Billed_Amount'].map(lambda x : x*2)
df.plot.scatter(x='Provider_ID', y='Billed_Amount', color='blue', title="Age vs. Claim Amount")
rolling()
.df['Rolling'] = df['Paid_Amount'].rolling(window=2).sum() # Calculating rolling sum.
df['Rolling'].rank()
df[(df['Provider_ID']>504) & (df['Status'] == 'Approved')]
.xs()
to slice data from a multi-index DataFrame.df_2024 = df.xs('2024', level='Year')
apply()
for custom transformations.df['Sales'] = df['Sales'].apply(lambda x : x*2 )
We hope this article was helpful for you and you learned a lot about data science from it. If you have friends or family members who would find it helpful, please share it to them or on social media.
Join our social media for more.
Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science Python for Data Science
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Welcome to Day 12 of Learning Python for Data Science. Today, we’ll dive into Pandas,…
NumPy Array in Python is a powerful library for numerical computing in Python. It provides…
Welcome to Day 9 of Learning Python for Data Science. Today we will explore comprehensions,…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…
Welcome to Day 8 of Learning Python for Data Science. Today we will explore Functions…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…