Skip to content Skip to sidebar Skip to footer

Adding Values To All Rows Of Dataframe

I have two pandas dataframes df1 (of length 2) and df2 (of length about 30 rows). Index values of df1 are always different and never occur in df2. I would like to add the average o

Solution 1:

When using mean on df1, it calculates over each column by default and produces a pd.Series.

When adding adding a pd.Series to a pd.DataFrame it aligns the index of the pd.Series with the columns of the pd.DataFrame and broadcasts along the index of the pd.DataFrame... by default.

The only tricky bit is handling the Date column.

Option 1

m=df1.mean()df2.loc[:,m.index]+=mdf2Datec1c2c3c4c5c6c1002017-09-12  1.51.02.651.452.53.03.312017-09-13  0.82.72.451.953.71.92.522017-10-10  2.11.82.751.452.62.93.132017-10-11  3.32.03.150.951.81.92.7

If I know that 'Date' is always in the first column, I can:

df2.iloc[:,1:]+=df1.mean()df2Datec1c2c3c4c5c6c1002017-09-12  1.51.02.651.452.53.03.312017-09-13  0.82.72.451.953.71.92.522017-10-10  2.11.82.751.452.62.93.132017-10-11  3.32.03.150.951.81.92.7

Option 2 Notice that I use the append=True parameter in the set_index just incase there are things in the index you don't want to mess up.

df2.set_index('Date', append=True).add(df1.mean()).reset_index('Date')

         Date   c1   c2    c3    c4   c5   c6  c10
02017-09-121.51.02.651.452.53.03.312017-09-130.82.72.451.953.71.92.522017-10-102.11.82.751.452.62.93.132017-10-113.32.03.150.951.81.92.7

If you don't care about the index, you can shorten this to

df2.set_index('Date').add(df1.mean()).reset_index()Datec1c2c3c4c5c6c1002017-09-12  1.51.02.651.452.53.03.312017-09-13  0.82.72.451.953.71.92.522017-10-10  2.11.82.751.452.62.93.132017-10-11  3.32.03.150.951.81.92.7

Solution 2:

If all columns are in both data frames, then just

for col in df2.columns:
    df2[col] = df2[col] + df1[col].mean()

if the columns are not necessarily in both then:

for col in df2.columns:if col in df1.columns:df2[col]=df2[col]+df1[col].mean()

Solution 3:

There is probably a more efficient way but here is a quick and dirty solution. I hope this helps!

d = {'c1': [0.5,0.7], 'c2': [0.6,1.2],'c3': [1.2,1.3]}
df1 = pd.DataFrame(data=d, index=['2017-09-10','2017-09-11'])
df2 = pd.DataFrame(data=d, index=['2017-09-12','2017-09-13'])

df1

Datec1c2c32017-09-10  0.50.61.22017-09-11  0.71.21.3

df2

Datec1c2c32017-09-12  0.50.61.22017-09-13  0.71.21.3

The averages of each column in df1 can be obtained using the describe() function

df1.describe().ix['mean']

c1    0.60
c2    0.90
c3    1.25

And now, simply add the series to df2

df2 + df1.describe().ix['mean']

Date     c1 c2  c3
2017-09-121.11.52.452017-09-131.32.12.55

Solution 4:

This could be another way of doing it , just simplified this a little bit

import pandas as pd
import numpy as np
from datetime import datetime, timedelta 
date_today=datetime.now()

#Creating df1 & df2 
df1=pd.DataFrame(
    {
        'Date':[date_today,date_today],
        'c1':[0.5,0.4],
        'c2':[0.6,0.3]
    }
)
df2=pd.DataFrame(
    {
        'Date':[date_today,date_today,date_today],
        'c1':[0.9,0.7,0.6],
        'c2':[0.8,0.4,0.3]
    }
)


#getting average of column c1
avg=df1["c1"].mean()

#Adding the average to your existing column of df2
df2['c1']+avg

Post a Comment for "Adding Values To All Rows Of Dataframe"