close

How to create a pandas DataFrame with specified number of rows and columns

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to create a pandas DataFrame with specified number of rows and columns in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to create a pandas DataFrame with specified number of rows and columns?

  1. How to create a pandas DataFrame with specified number of rows and columns?

    You can specify both index and columns to determine the shape. Values will default to NaN.

  2. create a pandas DataFrame with specified number of rows and columns

    You can specify both index and columns to determine the shape. Values will default to NaN.

Method 1

You can specify both index and columns to determine the shape. Values will default to NaN.

pd.DataFrame(index=np.arange(1), columns=np.arange(8))
     0    1    2    3    4    5    6    7
0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

Method 2

Yes, it is possible to create a dataframe of any shape. For example:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,10, size=(1,8)))

Yields:

   0  1  2  3  4  5  6  7
0  1  5  2  3  4  8  7  1

Then we can return the shape of this dataframe using df.shape:

(1, 8)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read