【问题标题】:AttributeError: 'Series' object has no attribute 'rows'AttributeError:“系列”对象没有属性“行”
【发布时间】:2019-04-22 02:59:19
【问题描述】:

我希望我的程序在他识别出一个字符串时打印前五个字符,该字符串由两列相加而成(来自用 pandas 制作的数据框),在 .txt 的某些行中,但正如在标题,当我运行代码时它给了我这个错误。这是代码(重要的行在代码的末尾,如果您想查看整个代码,我只是把所有内容都放了)。

import pandas as pd
import re
import numpy as np

link = "excelfilett.txt"
file = open(link, "r")
frames_load = []
is_count_frames_load = False
for line in file:
    if "[Interface1]" in line:
        is_count_frames_load = True
    if is_count_frames_load== True:
        frames_load.append(line)
    if "[EthernetComNeed]" in line:
        break

number_of_rows_load = len(frames_load) -1
header_load = re.split(r'\t', frames_load[0])
number_of_columns_load = len(header_load)

frame_array_load = np.full((number_of_rows_load, number_of_columns_load), 0)
df_frame_array_load = pd.DataFrame(frame_array_load)
df_frame_array_load.columns= header_load

for row in range(number_of_rows_load):
    frame_row_load = re.split(r'\t', frames_load[row])
    for position in range(len(frame_row_load))

df_frame_array_load["[Name]"] = df_frame_array_load["[End1]"] + "  " +  df_frame_array_load["[End2]"]


link = "excelfilett.txt"
file = open(link, "r")
frames_path = []
is_count_frames_path = False
for line in file:
    if "[Routing Paths]" in line:
        is_count_frames_path = True
    if is_count_frames_path== True:
        for row in df_frame_array_load["[Name]"].rows:
            if row in line:
                print(line[0:4])
    if "[EthernetComConfig]" in line:
        break

它在“for row in df_frame_array_load["[Name]"].rows:" 上给了我 AttributeError,它不应该是版本错误,那么问题是什么?我不明白。

【问题讨论】:

  • Pandas.Series 在最新的 0.23.4 版本的 pandas 中确实没有 "rows" 属性。 (我不知道它之前是否有属性“rows”)。
  • 确实,我认为它们是最新版本的行属性。

标签: python-3.x pandas dataframe


【解决方案1】:
for row in df_frame_array_load["[Name]"].rows:

因为 pandas Series 对象没有“行”属性,当您在 Series 中执行循环操作时,您正在对其进行迭代。

应该改为:

for row in df_frame_array_load["[Name]"]:
    ...

【讨论】:

  • 好的,谢谢,我不记得我在哪里看到的,但我认为 pandas 有一个 row 属性。
猜你喜欢
  • 2020-11-11
  • 2019-07-26
  • 2020-04-04
  • 2019-09-16
  • 2017-12-12
  • 2018-05-25
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多