AttributeError:“系列”对象没有属性“行”

时间:2018-11-19 17:34:42

标签: python-3.x pandas dataframe

我希望我的程序在.txt的某些行中识别出由两列相加(来自用pandas制成的数据框)组成的字符串时,打印出前五个字符。标题,当我运行代码时,它给了我这个错误。这是代码(重要的几行位于代码的末尾,如果您想查看整个代码,则只需输入所有内容即可。)

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

它给我“在df_frame_array_load [“ [Name]”]。rows:row中的行上的AttributeError”,这应该不是版本错误,那是什么问题呢?我不明白。

1 个答案:

答案 0 :(得分:0)

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

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

应更改为:

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