Which of the following takes a dict of dicts or a dict of array-like sequences and returns a DataFrame?

DataFrame.from_items
DataFrame.from_records
DataFrame.from_dict
All of the mentioned

The correct answer is D. All of the mentioned.

  • DataFrame.from_items takes a dict of dicts or a dict of array-like sequences and returns a DataFrame.
  • DataFrame.from_records takes a list of dicts and returns a DataFrame.
  • DataFrame.from_dict takes a dict and returns a DataFrame.

Here is an example of how to use DataFrame.from_items to create a DataFrame from a dict of dicts:

“`
import pandas as pd

df = pd.DataFrame.from_items({
“A”: {“a”: 1, “b”: 2},
“B”: {“c”: 3, “d”: 4},
})

print(df)

A B

0 1 3

1 2 4

“`

Here is an example of how to use DataFrame.from_records to create a DataFrame from a list of dicts:

“`
import pandas as pd

df = pd.DataFrame.from_records([
{
“a”: 1,
“b”: 2,
},
{
“c”: 3,
“d”: 4,
},
])

print(df)

a b

0 1 2

1 3 4

“`

Here is an example of how to use DataFrame.from_dict to create a DataFrame from a dict:

“`
import pandas as pd

df = pd.DataFrame.from_dict({
“a”: 1,
“b”: 2,
})

print(df)

a b

0 1 2

“`

Exit mobile version