【问题标题】:TypeError: 'int' object is not iterable when iterating through pandas columnTypeError: 'int' 对象在遍历 pandas 列时不可迭代
【发布时间】:2019-02-27 04:05:35
【问题描述】:

我有一个简单的用例。我想将文本文件读入熊猫文件并遍历唯一的 id 以绘制 xy 图。 在许多其他项目中为我工作得很好,但现在我得到了TypeError: 'int' object is not iterable。 起初我得到TypeError: 'numpy.float64' object is not iterable这就是为什么我将 id 的类型更改为 int (参见代码)。但这也不起作用。我不明白为什么。有什么想法吗?

f = open(file,"r+")
with open(file,"r+") as f1:
    data10 = f1.read()
    TESTDATA = StringIO(data11)
df = pd.read_table(TESTDATA, sep=" ")
df.columns = ["x", "y", "id"]

#Astype because i got the error TypeError: 'numpy.float64' object is not iterable
df.id = df.id.astype(int)

#get unique values of column id
list1=df['id'].tolist()
list1=list(set(list1))
fig, ax = plt.subplots()
for i ,g in list1:
    x = df[df.id==i]['x']
    y = df[df.id==i]['y']
    g.plot(x='x',y='x', marker='o', ax=ax, title="Evaluation")

【问题讨论】:

  • 你能告诉我data10和data11是什么吗?
  • 你期望 g 将包含什么 for i,g in list1: .. ?
  • 抱歉,这段代码在任何地方都无法正常工作。我必须这么清楚,因为我真的考虑过如何回答你的问题并意识到,我什至不知道从哪里开始......你可以在这里获得帮助,但请从你身边做两件事:be诚实并表现出自己的一些努力。
  • 也就是说,您的错误消息被抛出,因为您尝试在一个简单列表的 for 循环中使用两个变量。但是一个简单的列表每次迭代只提供一个,所以这是错误的。扔掉 for 循环中的, g
  • 但是,下一个错误很快就会出现...使用ax.plot() 而不是g.plot()。为什么你只绘制你的 x 数据,但超过 x y

标签: python pandas loops iteration typeerror


【解决方案1】:

恕我直言,您的代码简化为

df = pd.read_table(file, sep=" ")

fig, ax = plt.subplots()
grpd = df.groupby('id')
for i, g in grpd:
    g.plot(x='x',y='y', marker='o', ax=ax, title="Evaluation")

解释:
我会一步一步的看代码:

f = open(file,"r+")

可以删除with-block中打开文件之前不需要打开文件
但是如果你用这种方式打开它,不要忘记在你不再需要它之后关闭它。

with open(file,"r+") as f1:
    data10 = f1.read()
    TESTDATA = StringIO(data11)

可以删除
1. data10、data11(不起作用)和StringIO(太复杂)的奇怪用法
2. 看下一行代码

df = pd.read_table(file, sep=" ") 

好的如果你把TESTDATA简单地替换为file,pandas在导入不同风格的文件方面非常强大......

df.columns = ["x", "y", "id"]

好的如果您的文件还没有适合您需要的标头,我建议您检查一下...

#Astype because i got the error TypeError: 'numpy.float64' object is not iterable
df.id = df.id.astype(int)

可以删除,因为您的错误与此无关,正如我昨天在评论中所说的那样

#get unique values of column id
list1=df['id'].tolist()
list1=list(set(list1))

可以删除如果你想要pandas中某列的唯一值,使用df['id'].unique(),但是你不需要在这里手动做所有事情,至于你想要在那里实现的目标是groupby:

grpd = df.groupby('id')  

这会返回一个 groupby-object,当您对其进行迭代时,它确实会为您提供组名组数据,因此您的带有两个变量的循环可以在这里工作:

fig, ax = plt.subplots()
for i, g in grpd:
    g.plot(x='x',y='x', marker='o', ax=ax, title="Evaluation")

并且要完整:你的额外

x = df[df.id==i]['x']
y = df[df.id==i]['y']

也可以删除,因为这也包含在分组背后的想法中。 (顺便说一句,你甚至没有在你的代码中使用它......)

最后我建议你阅读 Python 和 pandas 的基础知识,尤其是文件 io https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files 和 pandas 分组 https://pandas.pydata.org/pandas-docs/stable/groupby.html

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 2021-04-25
    • 2017-09-11
    • 1970-01-01
    • 2023-01-22
    • 2018-09-29
    • 2020-02-26
    相关资源
    最近更新 更多