python - 错误 = '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”
我做错了什么或者有什么更好的方法来达到同样的结果?
解决方案
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
推荐阅读
- python-3.x - 我可以获取所有 Qt 不使用的命令行参数吗?
- javascript - 没有 iframe 的 Web 组件如何做出响应?
- javascript - 视图组件中的日期时间选择器
- git - 无法使用 Windows 身份验证和内部会员服务运行倭黑猩猩
- python - 使用 glob python 指定多个文件扩展名
- c# - 无法在 ASP .NET Core 中可视化 chartJS 数据
- php - (CRUD) 如何从 html 行中知道要定位哪些数据库记录
- firebase - 在前端生成文档 ID - 这是一种不好的做法吗?有哪些潜在的安全漏洞?
- node.js - 对 GCP VM.s 的安全 Socket.IO 访问
- python - python3 venv中自动安装pysvn