TypeError:'DataFrame' object is not callable

TypeError:'DataFrame' object is not callable

我一直在尝试将数据集拆分为训练数据和测试数据,以便使用 Streamlit 进行部署。

import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, KFold,cross_val_score
from sklearn.cluster import KMeans
import xgboost as xgb
from xgboost import XGBClassifier


def load_dataset():
    df = pd.read_csv('txn.csv')
    return df

df = load_dataset()


#create X and y, X will be feature set and y is the label - LTV
X = df.drop(['LTVCluster','m1_Revenue'],axis=1)
y = df(['LTVCluster'])

但是我在执行文件时收到此错误:

TypeError: 'DataFrame' 对象不可调用

Traceback:
File "c:\users\anish\anaconda3\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
    exec(code, module.__dict__)

File "C:\Users\Anish\Desktop\myenv\P52 - Retail Ecommerce\new1.py", line 25, in <module>
    y = df(['LTVCluster'],axis=1)

可能是什么错误??

对于select列,从df(['LTVCluster'])中删除():

y = df['LTVCluster']

你的最后一行有一组额外的括号,所以 Python 认为你在调用 df。要按 Pandas 中的列进行过滤,您需要使用方括号,因此请移除括号。

y = df['LTVCluster']