简体   繁体   中英

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

在此处输入图像描述

But, instead of these 2 columns ie 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?

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM