Complete the code to create a DataFrame from a dictionary.
import pandas as pd data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]} df = pd.DataFrame([1]) print(df)
The pd.DataFrame() function takes a dictionary as input to create a DataFrame. Here, data is the dictionary holding the data.
Complete the code to create a DataFrame from a list of lists with column names.
import pandas as pd rows = [[1, 'Apple'], [2, 'Banana'], [3, 'Cherry']] df = pd.DataFrame([1], columns=['ID', 'Fruit']) print(df)
pd.DataFrame instead of the data variable.The first argument to pd.DataFrame() is the data, here a list of lists named rows. The columns parameter names the columns.
Fix the error in the code to read a CSV file into a DataFrame.
import pandas as pd df = pd.read_csv([1]) print(df.head())
The file path must be a string, so it needs quotes around it. Without quotes, Python treats it as a variable which causes an error.
Fill both blanks to create a DataFrame from a dictionary with a condition filtering ages over 20.
import pandas as pd data = {'Name': ['Anna', 'Ben', 'Cara'], 'Age': [19, 22, 25]} df = pd.DataFrame([1]) filtered = df[df['Age'] [2] 20] print(filtered)
First, create the DataFrame from the dictionary data. Then filter rows where the 'Age' column is greater than 20 using the operator >.
Fill all three blanks to create a DataFrame from a list of dictionaries and select rows where score is above 80.
import pandas as pd records = [{'name': 'Tom', 'score': 75}, {'name': 'Lucy', 'score': 85}, {'name': 'Jake', 'score': 90}] df = pd.DataFrame([1]) high_scores = df[df[[2]] [3] 80] print(high_scores)
Create the DataFrame from the list of dictionaries records. Then filter rows where the 'score' column is greater than 80 using >.