Panda Coding Application Questions Flashcards
(15 cards)
Create a Pandas Series from the list [10, 20, 30, 40] with index labels [‘a’, ‘b’, ‘c’, ‘d’]. Print the Series.
import pandas as pd
ser = pd.Series([10, 20, 30, 40], index=[‘a’, ‘b’, ‘c’, ‘d’])
print(ser)
Using the DataFrame df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]}), print the ‘Age’ column.
Using the DataFrame df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]}), print the ‘Age’ column.
Create a DataFrame from the dictionary {‘A’: [1, 2], ‘B’: [3, 4]} and print it.
import pandas as pd
df = pd.DataFrame({‘A’: [1, 2], ‘B’: [3, 4]})
print(df)
Given a Series ser = pd.Series([1, 2, 3], index=[‘a’, ‘b’, ‘c’]), print the value at index ‘b’.
import pandas as pd
ser = pd.Series([1, 2, 3], index=[‘a’, ‘b’, ‘c’])
print(ser[‘b’])
Using the DataFrame from Question 2, display the first row using head().
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df.head(1))
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df.head(1))
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df[‘Salary’].mean())
Filter the DataFrame from Question 2 to show rows where ‘Salary’ is greater than 55000.
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df[df[‘Salary’] > 55000])
Given a DataFrame df = pd.DataFrame({‘A’: [1, 2, np.nan], ‘B’: [4, np.nan, 6]}), remove rows with any NaN values.
import pandas as pd
import numpy as np
df = pd.DataFrame({‘A’: [1, 2, np.nan], ‘B’: [4, np.nan, 6]})
print(df.dropna())
Concatenate two DataFrames df1 = pd.DataFrame({‘A’: [1, 2], ‘B’: [3, 4]}) and df2 = pd.DataFrame({‘A’: [5, 6], ‘B’: [7, 8]}) along rows.
import pandas as pd
df1 = pd.DataFrame({‘A’: [1, 2], ‘B’: [3, 4]})
df2 = pd.DataFrame({‘A’: [5, 6], ‘B’: [7, 8]})
result = pd.concat([df1, df2], axis=0)
print(result)
Merge two DataFrames df1 = pd.DataFrame({‘ID’: [1, 2], ‘Name’: [‘Alice’, ‘Bob’]}) and df2 = pd.DataFrame({‘ID’: [1, 2], ‘Salary’: [50000, 60000]}) on ‘ID’.
import pandas as pd
df1 = pd.DataFrame({‘ID’: [1, 2], ‘Name’: [‘Alice’, ‘Bob’]})
df2 = pd.DataFrame({‘ID’: [1, 2], ‘Salary’: [50000, 60000]})
result = pd.merge(df1, df2, on=’ID’, how=’inner’)
print(result)
Create a DataFrame with columns ‘Name’, ‘Age’, and ‘Score’ for 3 students and display its shape.
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [20, 22, 21], ‘Score’: [85, 90, 88]})
print(df.shape)
Using the DataFrame from Question 11, add a ‘Passed’ column where True if ‘Score’ >= 80, else False.
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [20, 22, 21], ‘Score’: [85, 90, 88]})
df[‘Passed’] = df[‘Score’] >= 80
print(df)
Given a DataFrame df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]}), use describe() to get summary statistics.
import pandas as pd
df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]})
print(df.describe())
Select rows from the DataFrame in Question 11 where ‘Age’ is greater than 20 using conditional selection.
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [20, 22, 21], ‘Score’: [85, 90, 88]})
print(df[df[‘Age’] > 20])
Reset the index of the DataFrame from Question 11 and print the result.
import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [20, 22, 21], ‘Score’: [85, 90, 88]})
df = df.reset_index()
print(df)