Python Forum
Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv'
#6
Ref desired_number = row[1]['Number_Header']
There is no Number_Header column in the csv file

https://pandas.pydata.org/pandas-docs/st...e.iterrows Wrote:Depending on the data types, the iterator returns a copy and not a view, and writing to it will have no effect.

Will need someone that understands pandas to help how to iterate and change data in place.

I found dataframe.at(), it works for 'Result_Header' but complains about 'Result_Notes' as its expecting a number not a string, so that column needs changing somehow.
import pandas as pd
  
data = pd.read_csv("some_data.csv")

  
for index, series in data.iterrows():
    #desired_number = row[1][5]
    # desired_number = series['Number_Header']
    data.at[index, 'Result_Header'] = 10

    # data.at[index, 'Result_Notes'] = 'This is your desired number' # ValueError: could not convert string to float: 'This is your desired number'
  
  
print(data)  
# data.to_csv("some_data_output.csv")
Output:
Account_Number Invoice_Date Invoice_Number Payor Ground_Tracking_ID_Prefix Express_or_Ground_Tracking_ID ... Standard Transit By Time Status Reason Exception Result_Header Result_Notes 0 X1231Z1123 4/13/2019 0000A031 P/P NaN 1Z112091091208143 ... 23:30:00 XX-XXX NaN NaN 10.0 NaN 1 X1231Z1123 4/13/2019 0000A031 P/P NaN 1Z112091091208144 ... 23:30:00 XX-XXX NaN NaN 10.0 NaN 2 X1231Z1123 4/13/2019 0000A031 P/P NaN 1Z112091091208145 ... 23:30:00 XX-XXX NaN NaN 10.0 NaN 3 X1231Z1123 4/13/2019 0000A031 P/P NaN 1Z112091091208146 ... 23:30:00 XX-XXX NaN NaN 10.0 NaN
Reply


Messages In This Thread
RE: Pandas to_csv in for loop - by Yoriz - Apr-22-2019, 02:30 PM
RE: Pandas to_csv in for loop - by NSearch - Apr-22-2019, 02:38 PM
RE: Pandas to_csv in for loop AttributeError: 'tuple' object has no attribute 'to_csv' - by Yoriz - Apr-22-2019, 03:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  error "list object has no attribute transpose()" usercat123 4 4,469 Jan-28-2022, 12:01 PM
Last Post: usercat123
  Python PDF merging from an excel pandas for loop siraero 0 2,273 Aug-16-2020, 09:34 AM
Last Post: siraero
  What is the mechanism of numpy function returning pandas object? Ibaraki 2 2,644 Apr-04-2020, 10:57 PM
Last Post: Ibaraki
  Python Pandas for loop/while loop question mrashy 1 3,982 Mar-24-2020, 04:39 AM
Last Post: deanhystad
  AttributeError: (“module 'pandas' has no attribute 'rolling_std'” Mariana136 4 7,772 Sep-23-2019, 12:56 PM
Last Post: Mariana136
  Loop pandas data frame by position ? Johnse 1 2,356 Sep-06-2019, 12:26 AM
Last Post: scidam
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 46,580 May-29-2019, 02:48 AM
Last Post: heiner55
  AttributeError: 'NoneType' object has no attribute 'all' synthex 2 5,417 Mar-07-2019, 11:11 AM
Last Post: synthex
  Please help with AttributeError: 'Netz' object has no attribute 'conv' DerBerliner 2 3,910 Feb-27-2019, 06:01 PM
Last Post: DerBerliner
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 15,854 Nov-04-2018, 10:38 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020