Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

python - Pandas for loop over dataframe gives too many values to unpack

I don't see why this code isn't working? I am trying to iterate over a data frame, which in this case only has one row in a for loop? There are only two columns and I have two for loop variables to take them? what am I missing please?

  print("process_list =  ",process_list)

  for row in process_list.itertuples():
       print("row = ", row)


  df_to_date = pd.DataFrame()

  try:
        print("process_list = {}  and it's type {}  process_list.itertuples() {} ".format(process_list, type(process_list),process_list.itertuples() ) )

        for   file_date , file_name  in process_list.itertuples(): # a whole batch of days 
               file_to_process = dev_env + file_name
               print("PROCESSING BATCH: ",file_to_process)
               df  = pd.read_csv(file_to_process, header=None,skiprows=22, sep=',', comment='*', converters = {"Days" : just_number,"Percentile" : just_number,"Date" : just_number} ,names = column_names )
               df.insert(0,'File_date',file_date)
               df_to_date = df_to_date.append(df)

  except Exception as e: 
           print ("nothing to process exception = ",e)
           sys.exit(0)

when I run it I get

process_list =       File_date          File_name
94   20180507  mcmhv20180507.csv
row =  Pandas(Index=94, File_date=20180507, File_name='mcmhv20180507.csv')
process_list =     File_date          File_name
94   20180507  mcmhv20180507.csv  and it's type <class 'pandas.core.frame.DataFrame'>  process_list.itertuples() <map object at 0x7f6339371e48> 
nothing to process exception =  too many values to unpack (expected 2)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

pd.DataFrame.itertuples returns an iterable of namedtuples including the index by default.

There are two options to account for this.

Option 1

Unpack 3 items instead of 2, the first of which you do not use.

Here is a minimal example:

df = pd.DataFrame([[10, 20], [30, 40], [50, 60]],
                  columns=['A', 'B'])

for idx, a, b in df.itertuples():
    print(idx, a, b)

0 10 20
1 30 40
2 50 60

In your case, a good convention to use would be to indicate an unused variable by _:

for _, file_date, file_name in process_list[['date', 'name']].itertuples():
    # do something

Option 2

Use index=False argument and unpack 2 elements:

for file_date, file_name in process_list[['date', 'name']].itertuples(index=False):
    # do something

The behaviour is indicated in the documentation:

DataFrame.itertuples(index=True, name='Pandas')

Iterate over DataFrame rows as namedtuples, with index value as first element of the tuple.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...