属性错误:“tuple”对象没有属性“to_csv”

问题描述 投票:0回答:1

我想将清理后的数据集导出到 CSV 中。我收到错误消息:


AttributeError                            Traceback (most recent call last)
Cell In\[57\], line 20
18 # Save the cleaned and updated dataset to a CSV file
19 output_file_path = r'D:\\Users\\Desktop\\ProjektAnalitiks\\data\\02_Interim\\dropped_rows_columns.csv'
\---\> 20 data_cleaned_dropped.to_csv(output_file_path, index=False)
22 print(f"Dataset saved to {output_file_path}")

AttributeError: 'tuple' object has no attribute 'to_csv'

部分代码:

# Columns to drop based on the missing percentage

columns_to_drop = \[
'Nazwa',
'Czas ostatniej modyfikacji',
'Jeżeli pominięto branżę, na której się znasz dopisz ją:',
'Jeżeli jest obszar, na którym się znasz i chcesz go wykorzystać, dopisz go:',
'Masz jakiś pomysł na projekt data? Napisz nam o tym. Jeżeli to nie ten moment, pozostaw puste pole.'
\]

# Dropping the specified columns from the cleaned dataset

data_cleaned_dropped = data_cleaned.drop(columns=columns_to_drop)

# Checking the size of the cleaned dataset after dropping columns

dataset_size = data_cleaned_dropped.shape

# Display the size of the cleaned dataset

print("Number of rows:", dataset_size\[0\])
print("Number of columns:", dataset_size\[1\])

# Display the first few rows of the updated dataset to confirm the columns are dropped

print(data_cleaned_dropped.head())

# Checking the size of the original dataset

original_dataset_size = data.shape

# Display the size of the original dataset

print("Number of rows:", original_dataset_size\[0\])
print("Number of columns:", original_dataset_size\[1\])

# Checking the size of the original dataset

data_cleaned_dropped = data_cleaned_dropped.shape

# Display the size of the original dataset

print("New number of rows:", data_cleaned_dropped\[0\])
print("New number of columns:", data_cleaned_dropped\[1\])

# Save the cleaned and updated dataset to a CSV file

output_file_path = r'D:\\Users\\Desktop\\ProjektAnalitiks\\data\\02_Interim\\dropped_rows_columns.csv'
data_cleaned_dropped.to_csv(output_file_path, index=False)

print(f"Dataset saved to {output_file_path}")

Exporting cleaned dataset to CSV.
python pandas dataframe dataset
1个回答
0
投票

错误消息

'tuple' object has no attribute 'to_csv'
指的是行

data_cleaned_dropped.to_csv(output_file_path, index=False)

这意味着在执行时,

data_cleaned_dropped
是一个
tuple
。这很奇怪,因为它第一次被分配时显然是一个
DataFrame
。要进行调试,请搜索该变量名称并查看它是否在错误发生之前的某个时间被重新分配。果然,

data_cleaned_dropped = data_cleaned_dropped.shape

现在是

shape
元组。该行没有做任何有用的事情。如果您在某个时候需要形状,它仍然在数据框上。只需将其删除即可。

© www.soinside.com 2019 - 2024. All rights reserved.