Skip to main content
add clause for multiple rows
Source Link

For anythe answer, I assume below:

  • Data frame has single row for each date in the past years

Set Date as index for the dataframe

df_dateInx = df.set_index('Date')

Now you can get a row for particular date using below code

df_row = df_dateInx.loc['2018-07-15']

Add a new column to dataframe 'ChangePercent' in the last

#df_dateInx.insert(inx_whr_col_to_insert, name_of_col)
df_dateInx.insert(df_row.shape[1], 'ChangePercent', True)

Create a function to calculate the different w.r.t. value the year before at the same day and month. This function would be invoked on each row of data frame

def calChange(row):
   change = 0
   val_prev_yr = df_dateInx.loc[row.Date - 1]['min']
   val_this_row = row['min']
   # do anything with values and return change
   return change

P.S. row.Date - 1 use date/time strptime function to do this

P.S. In case of multiple rows of same date use df_dateInx.loc[row.Date - 1]['min'][0] where [0] means selecting first row among many rows of same date

Invoke above function on each row of data frame

df_dateInx.agg([calChange])

And you would get a dataframe which has values populated in Change column as per your needs

For any answer, I assume below:

  • Data frame has single row for each date in the past years

Set Date as index for the dataframe

df_dateInx = df.set_index('Date')

Now you can get a row for particular date using below code

df_row = df_dateInx.loc['2018-07-15']

Add a new column to dataframe 'ChangePercent' in the last

#df_dateInx.insert(inx_whr_col_to_insert, name_of_col)
df_dateInx.insert(df_row.shape[1], 'ChangePercent', True)

Create a function to calculate the different w.r.t. value the year before at the same day and month. This function would be invoked on each row of data frame

def calChange(row):
   change = 0
   val_prev_yr = df_dateInx.loc[row.Date - 1]['min']
   val_this_row = row['min']
   # do anything with values and return change
   return change

P.S. row.Date - 1 use date/time strptime function to do this

Invoke above function on each row of data frame

df_dateInx.agg([calChange])

And you would get a dataframe which has values populated in Change column as per your needs

For the answer, I assume below:

  • Data frame has single row for each date in the past years

Set Date as index for the dataframe

df_dateInx = df.set_index('Date')

Now you can get a row for particular date using below code

df_row = df_dateInx.loc['2018-07-15']

Add a new column to dataframe 'ChangePercent' in the last

#df_dateInx.insert(inx_whr_col_to_insert, name_of_col)
df_dateInx.insert(df_row.shape[1], 'ChangePercent', True)

Create a function to calculate the different w.r.t. value the year before at the same day and month. This function would be invoked on each row of data frame

def calChange(row):
   change = 0
   val_prev_yr = df_dateInx.loc[row.Date - 1]['min']
   val_this_row = row['min']
   # do anything with values and return change
   return change

P.S. row.Date - 1 use date/time strptime function to do this

P.S. In case of multiple rows of same date use df_dateInx.loc[row.Date - 1]['min'][0] where [0] means selecting first row among many rows of same date

Invoke above function on each row of data frame

df_dateInx.agg([calChange])

And you would get a dataframe which has values populated in Change column as per your needs

Source Link

For any answer, I assume below:

  • Data frame has single row for each date in the past years

Set Date as index for the dataframe

df_dateInx = df.set_index('Date')

Now you can get a row for particular date using below code

df_row = df_dateInx.loc['2018-07-15']

Add a new column to dataframe 'ChangePercent' in the last

#df_dateInx.insert(inx_whr_col_to_insert, name_of_col)
df_dateInx.insert(df_row.shape[1], 'ChangePercent', True)

Create a function to calculate the different w.r.t. value the year before at the same day and month. This function would be invoked on each row of data frame

def calChange(row):
   change = 0
   val_prev_yr = df_dateInx.loc[row.Date - 1]['min']
   val_this_row = row['min']
   # do anything with values and return change
   return change

P.S. row.Date - 1 use date/time strptime function to do this

Invoke above function on each row of data frame

df_dateInx.agg([calChange])

And you would get a dataframe which has values populated in Change column as per your needs