排除 IF 函数中的两列

Excluding two columns in IF functions

我正在做一个我想从数据框中排除 'date' 和 'unix' 列的地方。

我该怎么做?

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)

为了测试多个值,可以使用 inlist:

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

Index.difference for all columns without specified in list, then is used DataFrame.apply 解决方案的另一个想法,删除了缺失的行并最后规范化:

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

如果要在 if 语句中添加第二个条件,则需要设置整个条件。

list_ = ["a", "b", "c", "d"]

for elem in list_:
    if elem != "a" and "b":
        print(elem)
# Output : b, c, d

for elem in list_:
    if elem != "a" and elem != "b":
        print(elem)
# Output : c, d

这翻译:

for col in data.columns:
  if col !=  'date' and col != 'unix' :
    print(col)