7

How to create an empty DataFrame and append rows & columns to it in Pandas?...

 3 years ago
source link: https://www.geeksforgeeks.org/how-to-create-an-empty-dataframe-and-append-rows-columns-to-it-in-pandas/?ref=leftbar-rightbar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to create an empty DataFrame and append rows & columns to it in Pandas?
  • search close
  • Sign In
  • Home
  • Courses
  • Algorithmskeyboard_arrow_down
  • Data Structureskeyboard_arrow_down
  • Languageskeyboard_arrow_down
  • Interview Cornerkeyboard_arrow_down
  • GATEkeyboard_arrow_down
  • CS Subjectskeyboard_arrow_down
  • Studentkeyboard_arrow_down
  • Jobskeyboard_arrow_down
  • GBlog
  • Puzzles
  • What's New ?

How to create an empty DataFrame and append rows & columns to it in Pandas?

Last Updated: 02-07-2020

Let’s discuss how to create an empty DataFrame and append rows & columns to it in Pandas. There are multiple ways in which we can do this task.

Method #1: Create a complete empty DataFrame without any column name or indices and then appending columns one by one to it.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
# create an Empty DataFrame object
df = pd.DataFrame()
print(df)
# append columns to an empty DataFrame
df['Name'] = ['Ankit', 'Ankita', 'Yashvardhan']
df['Articles'] = [97, 600, 200]
df['Improved'] = [2200, 75, 100]
df

Output:

python-pandas-empty-dataframe-1

Method #2: Create an empty DataFrame with columns name only then appending rows one by one to it using append() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
# create an Empty DataFrame
# object With column names only
df = pd.DataFrame(columns = ['Name', 'Articles', 'Improved'])
print(df)
# append rows to an empty DataFrame
df = df.append({'Name' : 'Ankit', 'Articles' : 97, 'Improved' : 2200}, 
ignore_index = True)
df = df.append({'Name' : 'Aishwary', 'Articles' : 30, 'Improved' : 50},
ignore_index = True)
df = df.append({'Name' : 'yash', 'Articles' : 17, 'Improved' : 220},
ignore_index = True)
df

Output:

python-empty-dataframe

Method #3: Create an empty DataFrame with a column name and indices and then appending rows one by one to it using loc[] method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import pandas library as pd
import pandas as pd
# create an Empty DataFrame object With
# column names and indices 
df = pd.DataFrame(columns = ['Name', 'Articles', 'Improved'], 
index = ['a', 'b', 'c'])
print("Empty DataFrame With NaN values : \n\n", df)
# adding rows to an empty 
# dataframe at existing index
df.loc['a'] = ['Ankita', 50, 100]
df.loc['b'] = ['Ankit', 60, 120]
df.loc['c'] = ['Harsh', 30, 60]
df

Output:

python-pandas-empty-dataframe

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
thumb_up
2
0

No votes yet.
Please write to us at [email protected] to report any issue with the above content.
Post navigation
Previous

first_page Loop or Iterate over all or certain columns of a dataframe in Python-Pandas last_page Get the number of rows and number of columns in Pandas Dataframe

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK