pandas AttributeError:'list' 对象没有属性 'size'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45098364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:59:59  来源:igfitidea点击:

AttributeError:'list' object has no attribute 'size'

pythonarrayspandastensorflow

提问by Raghavi

Here I am jus extracting a csv file and reading the "TV"values, calculating average and printing using tensorflow. But I am getting "AttributError" list has no attribute 'size' ". Can anyone please help me? Thanks in advance.

在这里,我只是提取一个 csv 文件并读取“TV”值,使用 tensorflow 计算平均值和打印。但是我收到“AttributError”列表没有属性“size”“。有人可以帮我吗?提前致谢。

 import tensorflow as tf
 import pandas
 csv = pandas.read_csv("Advertising.csv")["TV"]
 t = tf.constant(list(csv))
 r = tf.reduce_mean(t)
 sess = tf.Session()
 s = list(csv).size
 fill = tf.fill([s],r)
 f = sess.run(fill)
 print(f)

回答by Maxim

As summary of the discussion in the comments, here are valid ways of getting a length of the column in csv:

作为评论中讨论的摘要,以下是获取列长度的有效方法csv

$ csv = pandas.read_csv("Advertising.csv")
$ print type(csv), len(csv)
<class 'pandas.core.frame.DataFrame'> 10

$ series = csv["TV"]
$ print type(series), len(series)
<class 'pandas.core.series.Series'> 10

$ as_list = list(series)
$ print type(as_list), len(as_list)
<type 'list'> 10

And here's how to calculate the average (without tensorflow session):

以下是计算平均值的方法(没有 tensorflow 会话):

$ import numpy as np
$ print np.mean(series)
1.2