question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: extend DataFrame.insert to also insert rows

See original GitHub issue

Is your feature request related to a problem?

I wish I could use pandas to directly insert a row at a specific position in a data frame, just like I can insert a column via the DataFrame.insert method. This would be similar to the add_row function that exist for R data frames.

Describe the solution you’d like

expand the functionality of DataFrame.insert to take an axis argument and allow for the insertion of rows when axis is set to 0.

API breaking implications

as insert currently inserts a column by default, the default argument to axis should be set to 1. When this is done, I don’t foresee any breaks.

Describe alternatives you’ve considered

An alternative is to use concatenate. This is tedious though, because it requires several concat operations (upper block + row to insert + lower block) and error prone. Another alternative (not in-place) is to use np.insert like

import seaborn as sns
import pandas as pd
import numpy as np

def insert_row(df, row, position):
    out = pd.DataFrame(np.insert(arr=df.values,
        	                     obj=position,
                	             values=row,
                                 axis=0),
           	            columns=df.columns)

    return out
iris = sns.load_dataset("iris")
row_to_insert = iris.iloc[1, :]
insert_row(iris, row_to_insert, 1)

Additional context

Code example to insert column:

import seaborn as sns
iris = sns.load_dataset("iris")
col_to_insert = iris.iloc[:, 1]
position_to_insert = 1
iris.insert(position_to_insert, "new_column", col)

Desired behaviour:

row_to_insert = iris.iloc[1, :]
iris.insert(position_to_insert, "new_row", row, axis=0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rhshadrachcommented, Feb 6, 2021

Agreeing with @TomAugspurger, while the concat option might be a bit, it truly reflects what’s going on. If users really need this, it is easy to write a function that (I think) would be just as performant as what we could do internally.

0reactions
MarcoGorellicommented, May 31, 2021

closing as there doesn’t seem to be much support for this

Read more comments on GitHub >

github_iconTop Results From Across the Web

ENH: Add 'inplace' parameter to DataFrame.append() #2801
I have this data stored in another format taking ~5 million rows right now, "importing" it to a DataFrame is a one-time-heavy process...
Read more >
Pandas Add or Insert Row to DataFrame - Spark by {Examples}
By using append() function you can add or insert a row to existing pandas DataFrame from the dict. This method is required to...
Read more >
How to add one row in an existing Pandas DataFrame?
We can add a single row using DataFrame.loc. We can add the row at the last in our dataframe. We can get the...
Read more >
python - Is it possible to insert a row at an arbitrary position in ...
Is it possible to insert a row at an arbitrary position in a dataframe using pandas? · Possible to add row at particular...
Read more >
How to Add or Insert Row to Pandas DataFrame?
To append or add a row to DataFrame, create the new row as Series and use DataFrame.append() method. In this tutorial, we shall...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found