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 :

  1. df = pd.DataFrame([True, False, True], data = [5, 6, 7])
  2. df = pd.DataFrame(data = [5, 6, 7], index = [True, False, True])
  3. df = pd.DataFrame([true, false, true], data = [5, 6, 7])
  4. df = pd.DataFrame(index = [true, false, true], data = [[5, 6, 7]])

Python Pandas

2 Likes

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.

Answered By

3 Likes


Related Questions