Search code examples
pythonpython-3.xpandassklearn-pandas

Excluding two columns in IF functions


I'm doing a for where I want to exclude both 'date' and 'unix' columns from the data frame.

How can I do it?

from sklearn import preprocessing
from tensortrade.data.cdd import CryptoDataDownload 
import pandas as pd

cdd = CryptoDataDownload()

data = cdd.fetch("Bitstamp", "USD", "BTC", "1h")

for col in data.columns:
  if col !=  'date' and 'unix' :
    data[col]=data[col].pct_change()
    data.dropna(inplace=True)
    data[col] = preprocessing.scale(data[col].values)

Solution

  • For test multiple values is possible use in with list:

    if col not in ['date', 'unix']:
    

    Another idea for your solution with Index.difference for all columns without specified in list, then is used DataFrame.apply, removed missing rows and last normalized:

    cols = data.columns.difference(['date', 'unix'])
    data[cols]=data[cols].apply(lambda x: x.pct_change())
    data = data.dropna(subset=cols)
    data[cols]=data[cols].apply(lambda x: preprocessing.scale(x))