12

I am trying to insert or add from one dataframe to another dataframe. I am going through the original dataframe looking for certain words in one column. When I find one of these terms I want to add that row to a new dataframe.

I get the row by using. entry = df.loc[df['A'] == item] But when trying to add this row to another dataframe using .add, .insert, .update or other methods i just get an empty dataframe.

I have also tried adding the column to a dictionary and turning that into a dataframe but it writes data for the entire row rather than just the column value. So is there a way to add one specific row to a new dataframe from my existing variable ?

3 Answers 3

25

So the entry is a dataframe containing the rows you want to add? you can simply concatenate two dataframe using concat function if both have the same columns' name

import pandas as pd

entry = df.loc[df['A'] == item]
concat_df = pd.concat([new_df,entry])

pandas.concat reference:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

Sign up to request clarification or add additional context in comments.

Comments

3

The append function expect a list of rows in this formation:

[row_1, row_2, ..., row_N]

While each row is a list, representing the value for each columns

So, assuming your trying to add one row, you shuld use:

entry = df.loc[df['A'] == item]
df2=df2.append( [entry] )

Notice that unlike python's list, the DataFrame.append function returning a new object and not changing the object called it.

See also enter link description here

Comments

1

Not sure how large your operations will be, but from an efficiency standpoint, you're better off adding all of the found rows to a list, and then concatenating them together at once using pandas.concat, and then using concat again to combine the found entries dataframe with the "insert into" dataframe. This will be much faster than using concat each time. If you're searching from a list of items search_keys, then something like:

entries = []
for i in search_keys:
    entry = df.loc[df['A'] == item]
    entries.append(entry)

found_df = pd.concat(entries)
result_df = pd.concat([old_df, found_df])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.