Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add rows in dataframe
#1
How can I ad rows in the dataframe. I am getting the syntax for adding columns in the dataframe but not for the rows.

import pandas as pd
name=['First Name','last Name','College Name']
r=pd.DataFrame[columns=name]
Thanks.
Reply
#2
(Oct-02-2018, 08:49 PM)arya_starc Wrote: How can I ad rows in the dataframe. I am getting the syntax for adding columns in the dataframe but not for the rows.

You don't - pandas.DataFrame is an immutable object - you may change cell(s) content, but not the shape. Create list of either lists or dictionaries or named tuples, and create DataFrame in one call

import pandas as pd
name=['First Name','last Name','College Name']
data = [['John', 'Doe', 'Backwaters'], ....]
r=pd.DataFrame(data, columns=name)
You may use pandas.concat to combine DataFrames - but this is an inefficient operation.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas Dataframe Filtering based on rows mvdlm 0 1,397 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  change dataframe header with 2 rows tonycat 2 1,966 Oct-29-2020, 01:41 AM
Last Post: tonycat
  How to add a few empty rows into a pandas dataframe python_newbie09 2 16,288 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dataframe Rows Sorting stranger14u 1 2,554 Dec-17-2018, 11:47 PM
Last Post: scidam
  Write specific rows from pandas dataframe to csv file pradeepkumarbe 3 5,433 Oct-18-2018, 09:33 PM
Last Post: volcano63
  Get rows with same value from dataframe of particular columns angelwings 1 2,710 Apr-11-2018, 02:40 AM
Last Post: scidam
  Dropping rows in a dataframe sobrio1 0 2,604 Dec-03-2017, 11:15 PM
Last Post: sobrio1
  Stack dataframe columns into rows klllmmm 0 3,008 Sep-03-2017, 02:26 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020