Signature
pd.DataFrame(data=None, index=None, columns=None)What it does
A DataFrame stores columns that can have different dtypes while sharing one row index. You can construct one from a dictionary of columns, a list of records, or another table.
Parameters
datadict, list, ndarray, or Series, optionaloptional- The values that become the table.
indexIndex or array_like, optionaloptional- Row labels. A default RangeIndex is used if omitted.
columnsIndex or array_like, optionaloptional- Column labels, useful when data has no names.
Returns
DataFrame
Example
pythonpd.DataFrame example
import pandas as pd
df = pd.DataFrame({
"team": ["red", "blue", "red"],
"score": [10, 12, 8],
})
print(df)Output
team score 0 red 10 1 blue 12 2 red 8
Watch out for
- A dictionary of scalars needs an index; without one, pandas raises a ValueError.
- Column names are labels, not positions; use .iloc when you mean positions.