sam
sam

Reputation: 19204

Merging of values in the columns of a dataframe

I have a dataframe

import yfinance as yf
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
import pandas as pd

company_name = "INFY.NS"
df = yf.Ticker(company_name).history(period='400d', interval='1D')

Now, I have a dataframe as df1. I am doing calculations for get max and min values.

n = 2
df['min'] = df.iloc[argrelextrema(df['Close'].values, np.less_equal,order=n)[0]]['Close']
df['max'] = df.iloc[argrelextrema(df['Close'].values, np.greater_equal,order=n)[0]]['Close']
print()

Thus dataframe looks like this

enter image description here

But, instead of these 2 columns i.e. max and min, I want only one column named MACD and wanted to add values of max and min columns in it. Thus,

What is the best way to do this?

Upvotes: 0

Views: 43

Answers (1)

sam
sam

Reputation: 19204

I have got the answer to merge the columns and removing Nan columns, posting the code here.

df['total'] = df['min'].combine_first(df['max'])
df = df.dropna(subset=['total'])

Upvotes: 1

Related Questions