Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a series to existing DataFrame

Tags:

pandas

I created the following DataFrame:

purchase_1 = pd.Series({'Name': 'Chris',
                        'Item Purchased': 'Dog Food',
                        'Cost': 22.50})
purchase_2 = pd.Series({'Name': 'Kevyn',
                        'Item Purchased': 'Kitty Litter',
                        'Cost': 2.50})
purchase_3 = pd.Series({'Name': 'Vinod',
                        'Item Purchased': 'Bird Seed',
                        'Cost': 5.00})

df = pd.DataFrame([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1', 'Store 2'])

I then added the following column:

df['Location'] = df.index
df

How do I then add the following series to the my DataFrame? Thank you.

s = pd.Series({'Name':'Kevyn', 'Item Purchased': 'Kitty Food', 'Cost': 3.00, 'Location': 'Store 2'})
like image 684
martinbshp Avatar asked May 24 '17 10:05

martinbshp


People also ask

How do I add a series to a column in a DataFrame?

You can simply assign the values of your Series into the existing DataFrame to add a new column: series = pd. Series([40, 38, 32.5, 27, 30], index=[0, 1, 2, 3, 4])

Can you add series in Pandas?

Addition of Pandas series and otherThe add() function is used to add series and other, element-wise (binary operator add). Equivalent to series + other, but with support to substitute a fill_value for missing data in one of the inputs.


2 Answers

Use concat + to_frame + T:

df = pd.concat([df, s.to_frame().T])
print (df)
         Cost Item Purchased Location   Name
Store 1  22.5       Dog Food  Store 1  Chris
Store 1   2.5   Kitty Litter  Store 1  Kevyn
Store 2     5      Bird Seed  Store 2  Vinod
0           3     Kitty Food  Store 2  Kevyn

Also for default index is possible add parameter ignore_index=True:

df = pd.concat([df, s.to_frame().T], ignore_index=True)
print (df)
   Cost Item Purchased Location   Name
0  22.5       Dog Food  Store 1  Chris
1   2.5   Kitty Litter  Store 1  Kevyn
2     5      Bird Seed  Store 2  Vinod
3     3     Kitty Food  Store 2  Kevyn

Or add some new index value which is not in original df with loc:

df.loc[0] = s
print (df)
         Cost Item Purchased   Name Location
Store 1  22.5       Dog Food  Chris  Store 1
Store 1   2.5   Kitty Litter  Kevyn  Store 1
Store 2   5.0      Bird Seed  Vinod  Store 2
0         3.0     Kitty Food  Kevyn  Store 2

because else values are overwritten by Series:

df.loc['Store 2'] = s
print (df)
         Cost Item Purchased   Name Location
Store 1  22.5       Dog Food  Chris  Store 1
Store 1   2.5   Kitty Litter  Kevyn  Store 1
Store 2   3.0     Kitty Food  Kevyn  Store 2 <- overwritten row
like image 191
jezrael Avatar answered Oct 16 '22 14:10

jezrael


I hope it will be helpful and give you the accurate result,

purchase_4 = pd.Series({'Name': 'Kevyn', 
                        'Item Purchased': 'Kitty Food', 
                        'Cost': 3.00,
                       'Location': 'Store 2'})
df2 = df.append(purchase_4, ignore_index=True)
df2.set_index(['Location', 'Name'])
like image 23
sargupta Avatar answered Oct 16 '22 15:10

sargupta