Home > OS >  Pandas adding rows to dataframe
Pandas adding rows to dataframe

Time:09-10

I'm trying to add more rows or records to my data frame, let's say it looks like this:

ID   age   
44   23
31   25 

and I have a CSV file stored in another data frame without headers

33   55
22   23
29   22

now I want a new data frame that looks like this

ID   age   
44   23
31   25 
33   55
22   23
29   22

I have tried using append and concat but I didn't get the result that I wanted

CodePudding user response:

Assuming df1/df2, you can use set_axis to copy the axis of the first DataFrame, then concat:

out = pd.concat([df1, df2.set_axis(df1.columns, axis=1)], ignore_index=True)

output:

   ID  age
0  44   23
1  31   25
2  33   55
3  22   23
4  29   22

NB. ignore_index=True is optional, this is just to avoid having duplicated indices. Without it:

   ID  age
0  44   23
1  31   25
0  33   55
1  22   23
2  29   22

CodePudding user response:

A Dataframe has axes (indices) -- Row index (axis=0) --Column index (axes=1). Pandas provides various facilities for easily combining together Series, DataFrame.

pd.concat(objs, axis=0, join='outer', join_axes=None,ignore_index=False) 

      

• objs − This is a sequence or mapping of Series, DataFrame, or Panel objects.

• axis − {0, 1, ...}, default 0. This is the axis to concatenate along.

• join − {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for the intersection.

• ignore_index − boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, ..., n - 1.

• join_axes − This is the list of Index objects. Specific indexes to use for the other (n-1) axes instead of performing inner/outer set logic.

con = pd.concat([df1, df2]) # If column names are the same.
con = pd.concat([df1, df2], axis="columns") 

If indices are the same between datasets. If they’re different, by default the extra indices (rows) will also be added, and NaN values will be filled.

Two DataFrames might hold different kinds of information about the same entity and are linked by some common feature/column. To join these DataFrames, pandas provides multiple functions like merge(), join() etc.
If you have an SQL background, then you may use the merge operation names from the JOIN syntax. You can use Full Outer Join, Inner Join, RightJoin, Left Join,Joining on Index.

CodePudding user response:

import pandas as pd

# Your first df will be ‘df’

df_2 = pd.dataframe({“Id”: [33, 22, 29], “Age”: [55, 23, 22]})

df = df.append(df_2, ignore_index=True)

CodePudding user response:

You can use this, and ignore_index if you want.

output = pd.concat([df1, df2.set_axis(df1['columns'], axis=1)])
  • Related