首页 > 解决方案 > 错误 = 'str' 对象没有属性 'to_frame'

问题描述

免责声明:我是 Python 新手,所以这在很多方面可能都是错误的......

问题:我希望上传一个csv到python,将数据拆分成一个测试集和训练集,然后将测试集和训练集导出到两个单独的csv文件中。我有以下代码:

import pandas as pd
    
dataset = pd.read_csv('data.csv')

# Dividing dataset into label and feature sets
X = dataset.drop('Bankrupt?', axis = 1) # Features
Y = dataset['Bankrupt?'] # Labels

# Normalizing numerical features so that each feature has mean 0 and variance 1
feature_scaler = StandardScaler()
X_scaled = feature_scaler.fit_transform(X)

# Dividing dataset into training and test sets
X_train, X_test, Y_train, Y_test = train_test_split( X_scaled, Y, test_size = 0.3, random_state = 100)

#Export to csv
X_train = pd.DataFrame.to_csv(r'file path where csv is to be stored\training.csv', index = False)
X_test = pd.DataFrame.to_csv(r'file path where csv is to be stored\test.csv', index = False)

但我不断收到以下错误:“str”对象没有属性“to_frame”

我做错了什么或者有什么更好的方法来达到同样的结果?

标签: python

解决方案


pd.Dataframe 是数据框,而不是实例。 to_csv旨在成为从实例调用的实例方法。如果您将其称为类方法(如您所做的那样),则其第一个参数必须是数据框。

这些中的任何一个都应该起作用:

dataset.to_csv(r'file path ...)   # This is the intended use
pd.DataFrame.to_csv(dataset, r'file path ...)   # Harder to read and more prone to error

推荐阅读