简体   繁体   English

AttributeError: 模块“pandas”没有属性“to_csv”

[英]AttributeError: module 'pandas' has no attribute 'to_csv'

I took some rows from csv file like this我从这样的 csv 文件中取出了一些行

pd.DataFrame(CV_data.take(5), columns=CV_data.columns) 

and performed some functions on it.并对其执行了一些功能。 now i want to save it in csv again but it is giving error module 'pandas' has no attribute 'to_csv' I am trying to save it like this现在我想再次将它保存在 csv 中,但它给出了错误module 'pandas' has no attribute 'to_csv'我正在尝试像这样保存它

pd.to_csv(CV_data, sep='\t', encoding='utf-8') 

here is my full code.这是我的完整代码。 how can i save my resulting data in csv or excel?如何将结果数据保存在 csv 或 excel 中?

   # Disable warnings, set Matplotlib inline plotting and load Pandas package
import warnings
warnings.filterwarnings('ignore')

%matplotlib inline
import pandas as pd
pd.options.display.mpl_style = 'default' 

CV_data = sqlContext.read.load('Downloads/data/churn-bigml-80.csv', 
                          format='com.databricks.spark.csv', 
                          header='true', 
                          inferSchema='true')

final_test_data = sqlContext.read.load('Downloads/data/churn-bigml-20.csv', 
                          format='com.databricks.spark.csv', 
                          header='true', 
                          inferSchema='true')
CV_data.cache()
CV_data.printSchema() 

pd.DataFrame(CV_data.take(5), columns=CV_data.columns) 

from pyspark.sql.types import DoubleType
from pyspark.sql.functions import UserDefinedFunction

binary_map = {'Yes':1.0, 'No':0.0, True:1.0, False:0.0} 
toNum = UserDefinedFunction(lambda k: binary_map[k], DoubleType())

CV_data = CV_data.drop('State').drop('Area code') \
    .drop('Total day charge').drop('Total eve charge') \
    .drop('Total night charge').drop('Total intl charge') \
    .withColumn('Churn', toNum(CV_data['Churn'])) \
    .withColumn('International plan', toNum(CV_data['International plan'])) \
    .withColumn('Voice mail plan', toNum(CV_data['Voice mail plan'])).cache()

final_test_data = final_test_data.drop('State').drop('Area code') \
    .drop('Total day charge').drop('Total eve charge') \
    .drop('Total night charge').drop('Total intl charge') \
    .withColumn('Churn', toNum(final_test_data['Churn'])) \
    .withColumn('International plan', toNum(final_test_data['International plan'])) \
    .withColumn('Voice mail plan', toNum(final_test_data['Voice mail plan'])).cache()

pd.DataFrame(CV_data.take(5), columns=CV_data.columns) 

from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.tree import DecisionTree

def labelData(data):
    # label: row[end], features: row[0:end-1]
    return data.map(lambda row: LabeledPoint(row[-1], row[:-1]))

training_data, testing_data = labelData(CV_data).randomSplit([0.8, 0.2])

model = DecisionTree.trainClassifier(training_data, numClasses=2, maxDepth=2,
                                     categoricalFeaturesInfo={1:2, 2:2},
                                     impurity='gini', maxBins=32)

print (model.toDebugString())  
print ('Feature 12:', CV_data.columns[12])
print ('Feature 4: ', CV_data.columns[4] ) 

from pyspark.mllib.evaluation import MulticlassMetrics

def getPredictionsLabels(model, test_data):
    predictions = model.predict(test_data.map(lambda r: r.features))
    return predictions.zip(test_data.map(lambda r: r.label))

def printMetrics(predictions_and_labels):
    metrics = MulticlassMetrics(predictions_and_labels)
    print ('Precision of True ', metrics.precision(1))
    print ('Precision of False', metrics.precision(0))
    print ('Recall of True    ', metrics.recall(1))
    print ('Recall of False   ', metrics.recall(0))
    print ('F-1 Score         ', metrics.fMeasure())
    print ('Confusion Matrix\n', metrics.confusionMatrix().toArray()) 

predictions_and_labels = getPredictionsLabels(model, testing_data)

printMetrics(predictions_and_labels)  

CV_data.groupby('Churn').count().toPandas() 

stratified_CV_data = CV_data.sampleBy('Churn', fractions={0: 388./2278, 1: 1.0}).cache()

stratified_CV_data.groupby('Churn').count().toPandas() 

pd.to_csv(CV_data, sep='\t', encoding='utf-8') 

to_csv is a method of a DataFrame object, not of the pandas module. to_csvDataFrame对象的方法,而不是pandas模块的方法。

df = pd.DataFrame(CV_data.take(5), columns=CV_data.columns)

# whatever manipulations on df

df.to_csv(...)

You also have a line pd.DataFrame(CV_data.take(5), columns=CV_data.columns) in your code.您的代码中还有一行pd.DataFrame(CV_data.take(5), columns=CV_data.columns)

This line creates a dataframe and then discards it.此行创建一个数据帧,然后将其丢弃。 Even if you were successfully calling to_csv , none of your changes to CV_data would have been reflected in that dataframe (and therefore in the outputed csv file).即使您成功调用to_csv ,您对CV_data任何更改CV_data不会反映在该数据CV_data (因此也不会反映在输出的 csv 文件中)。

This will do the job!这将完成工作!

#Create a DataFrame:    
new_df = pd.DataFrame({'id': [1,2,3,4,5], 'LETTERS': ['A','B','C','D','E'], 'letters': ['a','b','c','d','e']})

#Save it as csv in your folder:    
new_df.to_csv('C:\\Users\\You\\Desktop\\new_df.csv')

Solution- You should write df.to_csv instead of pd.to_csv解决方案-您应该编写 df.to_csv 而不是 pd.to_csv

Justification- to_csv is a method to an object which is a df (DataFrame); Justification- to_csv 是一个对象的方法,它是一个 df (DataFrame); while pd is Panda module.而 pd 是 Panda 模块。

Hence, your code was not working and throwing this Error " AttributeError: module 'pandas' has no attribute 'to_csv'"因此,您的代码无法正常工作并抛出此错误“AttributeError: module 'pandas' has no attribute 'to_csv'”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'list' object 没有属性 'to_csv' - AttributeError: 'list' object has no attribute 'to_csv' AttributeError: 'str' 对象没有属性 'to_csv' - AttributeError: 'str' object has no attribute 'to_csv' Pandas:AttributeError:模块“csv”没有属性“excel” - Pandas: AttributeError: module 'csv' has no attribute 'excel' Pandas 'Str' 对象没有属性 'to_csv' - Pandas 'Str' object has no attribute 'to_csv' Python属性错误:AttributeError:'str'对象没有属性'to_csv' - Python attribute error: AttributeError: 'str' object has no attribute 'to_csv' Python AttributeError: 'list' object 没有属性 'to_csv' - Python AttributeError: 'list' object has no attribute 'to_csv' AttributeError("模块 'pandas' 没有属性 'read_csv'") - AttributeError("module 'pandas' has no attribute 'read_csv'") AttributeError:模块“ pandas”没有属性“ read_csv” - AttributeError: module 'pandas' has no attribute 'read_csv' 为什么我无法将输出数据帧转换为csv? AttributeError:'NoneType'对象没有属性'to_csv' - Why cant I get my output dataframe to convert to a csv? AttributeError: 'NoneType' object has no attribute 'to_csv' 在 python 中,我如何将结果保存到 csv,AttributeError: 'dict' object 没有属性 'to_csv' - in python , How I can save the result to csv , AttributeError: 'dict' object has no attribute 'to_csv'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM