user
How to create an empty dataframe with a given size in Python?
alphonsio

Use the following to create an empty dataframe with a given size (nRows x nCols) in Python / Pandas:

import pandas as pd
df = pd.DataFrame(index=range(nRows),columns=range(nCols))

In Python, you can create an empty DataFrame with a specified number of rows and columns using the pandas library. Here’s how you can do it:

Step-by-Step Instructions

  1. Import the pandas library:
    First, make sure to import the pandas library. If you don’t have it installed, you can install it using pip install pandas.

  2. Create the DataFrame:
    Use pd.DataFrame to create an empty DataFrame with the desired dimensions.

Here’s a simple example:

import pandas as pd

# Specify the number of rows and columns
num_rows = 5
num_columns = 3

# Create an empty DataFrame with the specified size
# We initialize it with NaN values using numpy
df = pd.DataFrame(index=range(num_rows), columns=range(num_columns))

# Optionally, you can replace NaN values with a specific value, e.g., 0 or an empty string
df.fillna('', inplace=True)

print(df)

Explanation:

  • index=range(num_rows): This specifies the range of indices (rows) for the DataFrame.
  • columns=range(num_columns): This specifies the range of column labels for the DataFrame.
  • df.fillna('', inplace=True): This step is optional. It fills all NaN values with an empty string or any value you prefer.

Output:

The output will be an empty DataFrame with 5 rows and 3 columns filled with empty strings:

   0  1  2
0         
1         
2         
3         
4         

You can also customize the column names if needed:

import pandas as pd

# Specify the number of rows and columns
num_rows = 5
num_columns = 3

# Specify custom column names
column_names = ['A', 'B', 'C']

# Create an empty DataFrame with the specified size and custom column names
df = pd.DataFrame(index=range(num_rows), columns=column_names)

# Optionally, fill NaN values with a specific value, e.g., 0 or an empty string
df.fillna('', inplace=True)

print(df)

Custom Column Names Output:

   A  B  C
0         
1         
2         
3         
4         

By following these steps, you can easily create an empty DataFrame with a specified size in Python using pandas.