ADVERTISEMENT
ADVERTISEMENT

Creating an empty Pandas DataFrame, then filling it?

Learn how to create an empty Pandas DataFrame, then filling it?
Submitted by Pranit Sharma, on April 13, 2022

DataFrames are 2-dimensional data structures in Pandas. DataFrames consist of rows, columns, and the data. DataFrame can be created with the help of python dictionaries but in the real world, CSV files are imported and then converted into DataFrames.

Now, we will see how to create an empty DataFrame. After creating DataFrame, we will append rows and columns in DataFrame.

To create an empty DataFrame, we will use the following syntax:

Syntax:

df=pandas.DataFrame()

Once the DataFrame is created, below is the syntax for appending new columns and rows to the DataFrame:

Syntax:

DataFrame['column1'] = ['val_1','val_2','val_3','val_4']

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Let us understand with the help of an example.

Example:

# Importing pandas package
import pandas as pd

# Creating an empty DataFrame
df = pd.DataFrame()

# Printing an empty DataFrame
print(df)

# Appending new columns and rows
df['Name'] = ['Raj','Simran','Prem','Priya']
df['Age'] = [30,29,32,22]
df['Gender'] = ['Male','Female','Male','Female']
df['Course'] = ['MBA','BA','BCA','MBBS']

# After creating rows and columns, 
# we will now print this DataFrame
print("\nFilled DataFrame\n",df)

Output:

Empty DataFrame
Columns: []
Index: []

Filled DataFrame
      Name  Age  Gender Course
0     Raj   30    Male    MBA
1  Simran   29  Female     BA
2    Prem   32    Male    BCA
3   Priya   22  Female   MBBS
Empty Pandas DataFrame | Output

Python Pandas Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.