df_train_set = pd.read_csv(train_data, names=columns) 这句话报错:argument of type 'method' is not iterable File "C:\Users\Dell\Desktop\数据处理作业lhq11111.py", line 11, in <module> df_train_set = pd.read_csv(train_data, names=columns) TypeError: argument of type 'method' is not iterable,请为我解释一下为什么及如何修改
时间: 2024-03-12 20:46:55 浏览: 140
这个错误提示是因为在这行代码中,`train_data` 是一个 pandas DataFrame 对象,而 `pd.read_csv` 函数的第一个参数应该是 csv 文件的路径,而不是 DataFrame 对象。因此,应该将这行代码修改为:
```python
df_train_set = pd.read_csv("C://Users//Dell//Desktop//数据分析作业//adult_train(1).csv", names=columns)
```
同理,将 `test_data` 的读取代码修改为:
```python
df_test_set = pd.read_csv("C://Users//Dell//Desktop//数据分析作业//adult_test.csv", names=columns, skiprows=1)
```
这样应该就可以正确读取训练集和测试集数据了。
相关问题
import numpy as np import pandas as pd train_data = pd.read_csv("C://Users//Dell//Desktop//数据分析作业//adult_train(1).csv") test_data = pd.read_csv("C://Users//Dell//Desktop//数据分析作业//adult_test.csv") #写入csv文件 columns = ['Age','Workclass','fnlgwt','Education','EdNum','MaritalStatus', 'Occupation','Relationship','Race','Sex','CapitalGain', 'CapitalLoss','HoursPerWeek','Country','Income'] #写入名称 df_train_set = pd.read_csv('C://Users//Dell//Desktop//数据分析作业//adult_train(1).csv', names=columns) #跳过表头 df_test_set = pd.read_csv('C://Users//Dell//Desktop//数据分析作业//adult_test.csv', names=columns, skiprows=1) #删除ID序列号栏 df_train_set.drop('fnlgwt', axis=1, inplace=True) df_test_set.drop('fnlgwt', axis=1, inplace=True) #用unknown替换? for i in df_train_set.columns: df_train_set[i].replace('?', 'Unknown', inplace=True) df_test_set[i].replace('?', 'Unknown', inplace=True) #去掉非int64类型数据中的点和空格 for col in df_train_set.columns: if df_train_set[col].dtype != 'int64': df_train_set[col] = df_train_set[col].apply(lambda val: val.replace(" ", "")) df_train_set[col] = df_train_set[col].apply(lambda val: val.replace(".", "")) df_test_set[col] = df_test_set[col].apply(lambda val: val.replace(" ", "")) df_test_set[col] = df_test_set[col].apply(lambda val: val.replace(".", "")) # 将训练集导出为 csv 文件 df_train_set.to_csv('train_set.csv', index=False) # 将测试集导出为 csv 文件 df_test_set.to_csv('test_set.csv', index=False)
这段代码是用来读取和预处理成人收入数据集(Adult dataset)的。其中包括读取训练集和测试集的csv文件,去掉ID序列号栏,用"Unknown"替换缺失值"?",去除非int64类型数据中的点和空格,并将处理后的数据集导出为csv文件。该数据集是一个二分类问题,目标是预测一个人的收入是否超过50K美元。这段代码中的预处理是为了对数据进行清洗和转换,以便更好地进行建模和预测。
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--path', type=str, default=r"data/UCI HAR Dataset/UCI HAR Dataset", help='UCI dataset data path') parser.add_argument('--save', type=str, default='data/UCI_Smartphone_Raw.csv', help='save file name') args = parser.parse_args() data_path = args.path # read train subjects train_subjects = pd.read_csv(os.path.join(data_path, 'train/subject_train.txt'), header=None, names=['subject']) # read test subjects test_subjects = pd.read_csv(os.path.join(data_path, 'test/subject_test.txt'), header=None, names=['subject']) # concat subjects = pd.concat([train_subjects, test_subjects], axis=0) # read train labels train_labels = pd.read_csv(os.path.join(data_path, 'train/y_train.txt'), header=None, names=['label']) # read train labels test_labels = pd.read_csv(os.path.join(data_path, 'test/y_test.txt'), header=None, names=['label']) # labels labels = pd.concat([train_labels, test_labels], axis=0) final_dataframe = pd.concat([subjects, labels], axis=1) data = [] for name in COLUMNS: final_dataframe = pd.concat([final_dataframe, read_txt(name)], axis=1) final_dataframe.to_csv(args.save,index=False) 如何将文中txt文件改成mnist数据集数据,其他不做大修改
要将代码修改为使用MNIST数据集而不是txt文件,你需要进行以下修改:
1. 导入所需的库和模块:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_openml
```
2. 加载MNIST数据集:
```python
mnist = fetch_openml('mnist_784', version=1, cache=True)
```
3. 将图像矩阵和标签分别存储为numpy数组:
```python
X = mnist.data
y = mnist.target
```
4. 创建一个包含所有数据的Pandas数据框:
```python
df = pd.DataFrame(X)
df['label'] = y
```
5. 将数据框保存为CSV文件:
```python
df.to_csv(args.save, index=False)
```
注意,这里我们使用了`args.save`作为保存文件名,这是根据你之前的代码中的`--save`命令行参数来决定的。
除了以上修改,你还需要删除以下代码块,因为MNIST数据集已经包含了图像数据和标签,不需要再读取其他文件:
```python
# read train subjects
train_subjects = pd.read_csv(os.path.join(data_path, 'train/subject_train.txt'), header=None, names=['subject'])
# read test subjects
test_subjects = pd.read_csv(os.path.join(data_path, 'test/subject_test.txt'), header=None, names=['subject'])
# concat
subjects = pd.concat([train_subjects, test_subjects], axis=0)
# read train labels
train_labels = pd.read_csv(os.path.join(data_path, 'train/y_train.txt'), header=None, names=['label'])
# read train labels
test_labels = pd.read_csv(os.path.join(data_path, 'test/y_test.txt'), header=None, names=['label'])
# labels
labels = pd.concat([train_labels, test_labels], axis=0)
final_dataframe = pd.concat([subjects, labels], axis=1)
data = []
for name in COLUMNS:
final_dataframe = pd.concat([final_dataframe, read_txt(name)], axis=1)
```
这些修改后的代码将直接将MNIST数据集转换为CSV文件,并保存到指定的路径中。
阅读全文