logo_learn_stats

Pandas: Methods to Form Uninhabited DataFrame with Column Names

Posted on
banner 336x280

You’ll be able to importance please see plain syntax to develop an unoccupied pandas DataFrame with particular column names:

df = pd.DataFrame(columns=['Col1', 'Col2', 'Col3'])

Refer to examples displays how you can importance this syntax in observe.

banner 468x60

Instance 1: Form DataFrame with Column Names & Refuse Rows

Refer to code displays how you can develop a pandas DataFrame with particular column names and disagree rows:

import pandas as pd

#develop DataFrame
df = pd.DataFrame(columns=['A', 'B', 'C', 'D', 'E'])

#view DataFrame
df

A   B   C   D   E


We will importance state to get the dimensions of the DataFrame:

#show state of DataFrame
df.state

(0, 5)

This tells us that the DataFrame has 0 rows and 5 columns.

We will additionally importance record() to get an inventory of the column names:

#show record of column names
record(df)

['A', 'B', 'C', 'D', 'E']

Instance 2: Form DataFrame with Column Names & Particular Choice of Rows

Refer to code displays how you can develop a pandas DataFrame with particular column names and a particular choice of rows:

import pandas as pd

#develop DataFrame
df = pd.DataFrame(columns=['A', 'B', 'C', 'D', 'E'],
                  index=area(1, 10))
#view DataFrame
df

        A	B	C	D	E
1	NaN	NaN	NaN	NaN	NaN
2	NaN	NaN	NaN	NaN	NaN
3	NaN	NaN	NaN	NaN	NaN
4	NaN	NaN	NaN	NaN	NaN
5	NaN	NaN	NaN	NaN	NaN
6	NaN	NaN	NaN	NaN	NaN
7	NaN	NaN	NaN	NaN	NaN
8	NaN	NaN	NaN	NaN	NaN
9	NaN	NaN	NaN	NaN	NaN

Realize that each worth within the DataFrame is stuffed with a NaN worth.

As soon as once more, we will importance state to get the dimensions of the DataFrame:

#show state of DataFrame
df.state

(9, 5)

This tells us that the DataFrame has 9 rows and 5 columns.

Spare Assets

Refer to tutorials provide an explanation for how you can carry out alternative familiar operations in pandas:

Methods to Form Pristine Column In line with Situation in Pandas
Methods to Insert a Column Right into a Pandas DataFrame
Methods to Poised Column as Index in Pandas

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *