Home > Software engineering >  How to store the minimum value of three different Series in a new Series?
How to store the minimum value of three different Series in a new Series?

Time:05-05

here you are the code:

import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

ticker = "EURUSD=X"
df = yf.download(ticker, start='2021-1-1', interval='1h')

df['MA30'] = df.Close.rolling(30).mean()
df['MA60'] = df.Close.rolling(60).mean()
df['MA120'] = df.Close.rolling(120).mean()
df['ADX'] = ta.trend.ADXIndicator(df.High, df.Low, df.Close, window=14).adx()
df = df.dropna()

How can I get the minimum (of every row) among MA30, MA60 and MA120 and store it in a new column which I may call "ma_min"? Thanks

CodePudding user response:

new_col = df.apply(lambda row: min(row["MA30"], row["MA60"], row["MA120"]), axis=1)

df["mins"] = new_col

CodePudding user response:

import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 150)

ticker = "EURUSD=X"
df = yf.download(ticker, start='2021-1-1',interval='1h')
df['MA30'] = df['Close'].rolling(window=30).mean()
df['MA60'] = df['Close'].rolling(window=60).mean()
df['MA120'] = df['Close'].rolling(window=120).mean()


df['ma_min'] = df[['MA30','MA60', 'MA120']].min(axis=1)
print(df.head(400))
  • Related