Reverse-searching excel rows and appending positive integers

时间:2019-03-17 22:43:42

标签: python pandas

I'm trying to, row by row, append any positive numbers to a list until the list has 5 items. Then, for each row searched, place each list within a meta-list holding all of them.

import pandas as pd
import easygui

df_name = easygui.fileopenbox() 
thesis_df = pd.ExcelFile(df_name)
df = pd.read_excel(thesis_df)

meta_seq = []
for index, row in df.iterrows():
    sequence = []
    for item in row[::-1]:
       while len(sequence) < 5:
            if item.isdigit() == True and item > 0:
                sequence.append(item)
            else:
                continue
            sequence = sequence.reverse()
            meta_seq.append(sequence)

Currently, when I run the code, I get back an error saying:

AttributeError: 'float' object has no attribute 'isdigit'

1 个答案:

答案 0 :(得分:0)

The isdigit method is supposed to be used with strings, not floats. Try using either

if str(item).isdigit() == True and item > 0:
    ...

or

if isinstance(item, float) and item > 0:
    ...