BetterDocs
Home
Docs

Creation | pd.DataFrame()

Method:

pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

The constructor creates a two-dimensional labeled data structure (DataFrame) from various input data types.

Returns:

pandas.core.frame.DataFrame

Parameters:

data: (array-like, iterable, dict, scalar), Optional-

Input data to populate the DataFrame (e.g., dictionary, ndarray, DataFrame, etc.).

import pandas as pd

# Creating a DataFrame
data = {'Name': ['Better', 'Docs'], 'Age': [25, 30]}
df = pd.DataFrame(data=data)
print(df)
'''
Output:
     Name  Age
0  Better   25
1    Docs   30
'''

If data is empty, it will return an empty DataFrame.

index: array-like, Optional-

A sequence of labels for the data. If None, a default integer index (0, 1, 2, ...) is assigned.

import pandas as pd

# Creating a DataFrame
data = {'Name': ['Better', 'Docs'], 'Age': [25, 30]}
df = pd.DataFrame(data=data, index=["Row1", "Row2"])
print(df)
'''
Output:
        Name  Age
Row1  Better   25
Row2    Docs   30
'''

If index is provided, it must always match with the length of the data.

columns: array-like, Optional-

Specifies column labels. If None, a default integer index (0, 1, 2, ...) is assigned.

import pandas as pd

# Creating a DataFrame
data = {'Name': ['Better', 'Docs'], 'Age': [25, 30]}
df = pd.DataFrame(data=data, index=("Row1", "Row2"), columns=["Name", "Age"])
print(df)
'''
Output:
        Name  Age
Row1  Better   25
Row2    Docs   30
'''

The names in the columns must always match the names in the data.

If it does not match, the values for that particular column will be NaN.

dtype: data-type, Optional-

Specifies the data-type of the DataFrame. If not provided, it’s inferred from the input.

import pandas as pd

# Creating a DataFrame
data = {'Name': ['Better', 'Docs'], 'Age': [25, 30]}
df = pd.DataFrame(data=data, index=("Row1", "Row2"), columns=["Name", "Age"], dtype='object')
print(df)
'''
Output:
        Name  Age
Row1  Better   25
Row2    Docs   30
'''

Values: +

copy: (True or False) Optional-

If True, a copy of the input data is created. This is useful if you want to ensure the original data remains unchanged.

copy = False (default) +

copy = True +

copy is handy only when the data is of the type np.ndarray.

Modification to the original_array or new_array will affect both the arrays.

Refer to numpy  documentation.


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy