0

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,

  • if max is none and min has value, add in MACD column and vice versa
  • if max and min are both Nan, drop the row.

What is the best way to do this?

3
  • 1
    It's recommended to add your data as text, we can not copy the contents from image. Commented May 22, 2021 at 6:12
  • Rather than adding a data frame, only by running the lines of code, all data is available @ThePyGuy. That's why gave the code, kindly check Commented May 22, 2021 at 6:21
  • 1
    And that requires one to install yfinance. Anyways, it's about preferences, that was just a gentle suggestion. Commented May 22, 2021 at 6:25

1 Answer 1

1

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'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.