Panda Coding Application Questions Flashcards

(15 cards)

1
Q

Create a Pandas Series from the list [10, 20, 30, 40] with index labels [‘a’, ‘b’, ‘c’, ‘d’]. Print the Series.

A

import pandas as pd
ser = pd.Series([10, 20, 30, 40], index=[‘a’, ‘b’, ‘c’, ‘d’])
print(ser)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Using the DataFrame df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]}), print the ‘Age’ column.

A

Using the DataFrame df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]}), print the ‘Age’ column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create a DataFrame from the dictionary {‘A’: [1, 2], ‘B’: [3, 4]} and print it.

A

import pandas as pd
df = pd.DataFrame({‘A’: [1, 2], ‘B’: [3, 4]})
print(df)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given a Series ser = pd.Series([1, 2, 3], index=[‘a’, ‘b’, ‘c’]), print the value at index ‘b’.

A

import pandas as pd
ser = pd.Series([1, 2, 3], index=[‘a’, ‘b’, ‘c’])
print(ser[‘b’])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using the DataFrame from Question 2, display the first row using head().

A

import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df.head(1))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df.head(1))

A

import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df[‘Salary’].mean())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Filter the DataFrame from Question 2 to show rows where ‘Salary’ is greater than 55000.

A

import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’], ‘Age’: [25, 30], ‘Salary’: [50000, 60000]})
print(df[df[‘Salary’] > 55000])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given a DataFrame df = pd.DataFrame({‘A’: [1, 2, np.nan], ‘B’: [4, np.nan, 6]}), remove rows with any NaN values.

A

import pandas as pd
import numpy as np
df = pd.DataFrame({‘A’: [1, 2, np.nan], ‘B’: [4, np.nan, 6]})
print(df.dropna())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Concatenate two DataFrames df1 = pd.DataFrame({‘A’: [1, 2], ‘B’: [3, 4]}) and df2 = pd.DataFrame({‘A’: [5, 6], ‘B’: [7, 8]}) along rows.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Merge two DataFrames df1 = pd.DataFrame({‘ID’: [1, 2], ‘Name’: [‘Alice’, ‘Bob’]}) and df2 = pd.DataFrame({‘ID’: [1, 2], ‘Salary’: [50000, 60000]}) on ‘ID’.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Create a DataFrame with columns ‘Name’, ‘Age’, and ‘Score’ for 3 students and display its shape.

A

import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [20, 22, 21], ‘Score’: [85, 90, 88]})
print(df.shape)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using the DataFrame from Question 11, add a ‘Passed’ column where True if ‘Score’ >= 80, else False.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given a DataFrame df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]}), use describe() to get summary statistics.

A

import pandas as pd
df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6]})
print(df.describe())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Select rows from the DataFrame in Question 11 where ‘Age’ is greater than 20 using conditional selection.

A

import pandas as pd
df = pd.DataFrame({‘Name’: [‘Alice’, ‘Bob’, ‘Charlie’], ‘Age’: [20, 22, 21], ‘Score’: [85, 90, 88]})
print(df[df[‘Age’] > 20])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Reset the index of the DataFrame from Question 11 and print the result.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly