[965] Generate a new empty DataFrame with the same columns as an existing DataFrame in Pandas

alex_bn_lee / 2024-02-08 / 原文

To generate a new empty DataFrame with the same columns as an existing DataFrame in Pandas, you can use the pd.DataFrame constructor and pass the columns from the existing DataFrame. Here's an example:

import pandas as pd

# Sample DataFrame
existing_df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Create a new empty DataFrame with the same columns
new_empty_df = pd.DataFrame(columns=existing_df.columns)

print(new_empty_df)

In this example, pd.DataFrame(columns=existing_df.columns) creates a new empty DataFrame (new_empty_df) with the same columns as the existing_df.

Adjust the columns and other parameters based on your specific use case.