Informatics Practices
Sudhanshu has written the following code to create a DataFrame with boolean index :
import numpy as np
import pandas as pd
df = pd.DataFrame(data = [[5, 6, 7]], index = [true, false, true])
print(df)
While executing the code, she is getting an error, help her to rectify the code :
- df = pd.DataFrame([True, False, True], data = [5, 6, 7])
- df = pd.DataFrame(data = [5, 6, 7], index = [True, False, True])
- df = pd.DataFrame([true, false, true], data = [5, 6, 7])
- df = pd.DataFrame(index = [true, false, true], data = [[5, 6, 7]])
Answer
df = pd.DataFrame(data = [5, 6, 7], index = [True, False, True])
Reason — The index values 'true' and 'false' should have the first letter capitalized to match Python's boolean values. Also, the 'data' parameter should contain the list of values to be included in the DataFrame. Hence, df = pd.DataFrame(data = [5, 6, 7], index = [True, False, True]) is correct.
Related Questions
To delete a column from a DataFrame, you may use …………… statement.
- remove
- del
- drop
- cancel
To delete a row from a DataFrame, you may use …………… statement.
- remove
- del
- drop
- cancel
Fill in the blanks:
_________ is a popular data-science library of Python.
Fill in the blanks:
A _________ is a Pandas data structure that represents a 1 D array like object.