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'
#1
Hi all,

Thank you in advance for all suggestions. I am attempting to read data from a csv file and populate a field ('Result_Header') with a value. The example below is just to help me understand the concept -- the final code will actually be populating a value from an api call (that part I figured out). I am just getting tripped up on the to_csv function.

print(data) shows me that the column is being populated correctly, it is just the to_csv where I get the error below.

import pandas as pd

data = pd.read_csv("C:\\Users\\X\\Documents\\X\\some_data.csv")

for data in data.iterrows():

    #desired_number = data[1][5]
    desired_number = data[1]['Number_Header']
    data[1]['Result_Header'] = desired_number
    data[1]['Result_Notes'] = 'This is your desired number'

    print(data) 


data.to_csv("C:\\Users\\X\\Documents\\X\\some_data_output.csv")
Error:
AttributeError: 'tuple' object has no attribute 'to_csv'
Reply
#2
data = pd.read_csv("C:\\Users\\X\\Documents\\X\\some_data.csv")
In the above data is assigned as a pandas dataframe by reading in from a csv file.
but in the following line data is overwritten when it is used as the variable for each iteration of data.iterrows()
for data in data.iterrows():
When you try using
data.to_csv("C:\\Users\\X\\Documents\\X\\some_data_output.csv")
it is no longer a dataframe its a tuple

Try changing the following
import pandas as pd
 
data = pd.read_csv("C:\\Users\\X\\Documents\\X\\some_data.csv")
 
for row in data.iterrows():
 
    #desired_number = row[1][5]
    desired_number = row[1]['Number_Header']
    row[1]['Result_Header'] = desired_number
    row[1]['Result_Notes'] = 'This is your desired number'
 
    print(row) 
 
 
data.to_csv("C:\\Users\\X\\Documents\\X\\some_data_output.csv")
Reply
#3
@Yoriz,

Thank you for the response. The code now runs without error, but the output file does not contain the values that I expected in 'Result_Header' and 'Result_Notes' (they are still null).
Reply
#4
what does some_data.csv contain
Reply
#5
Please let me know if this is an incorrect way to post.

Output:
Account_Number,Invoice_Date,Invoice_Number,Payor,Ground_Tracking_ID_Prefix,Express_or_Ground_Tracking_ID,Transportation_Charge_Amount,Net_Charge_Amount,Declared_Value,Service_Type,Ground_Service,Shipment_Date,POD_Delivery_Date,POD_Delivery_Time,POD_Signature_Description,Actual_Weight_Amount,Actual_Weight_Units,Rated_Weight_Amount,Rated_Weight_Units,Number_of_Pieces,Service_Packaging,Shipper_Company,Shipper_Name,Shipper_Address_Line_1,Shipper_Address_Line_2,Shipper_City,Shipper_State,Shipper_Postal_Code,Shipper_Country_Territory,Recipient_Name,Recipient_Company,Recipient_Address_Line_1,Recipient_Address_Line_2,Recipient_City,Recipient_State,Recipient_Postal_Code,Recipient_Country_Territory,API_Pickup_Date,API_First_Activity_Date,API_First_Activity_Time, API Delivery Date,API Delivery Time,First Attempt Date, First Attempt Time, Standard Transit By Date,Standard Transit By Time,Status,Reason,Exception,Result_Header,Result_Notes X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208143,,7.54,0,Standard,,,,,,25.3,L,26,L,1,PKG,XXXXXXXXXX,,XXXXXXXXXXXX,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:02:00,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,, X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208144,,7.54,0,Standard,,,,,,25.2,L,26,L,1,PKG,,,,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:03:30,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,, X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208145,,7.25,0,Standard,,,,,,24.7,L,25,L,1,PKG,,,,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:02:33,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,, X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208146,,4.64,0,Standard,,,,,,15,L,16,L,1,PKG,,,,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:02:12,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,,
Reply
#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
#7
I am sorry, some_data.csv should be:

Output:
Account_Number,Invoice_Date,Invoice_Number,Payor,Ground_Tracking_ID_Prefix,Number_Header,Transportation_Charge_Amount,Net_Charge_Amount,Declared_Value,Service_Type,Ground_Service,Shipment_Date,POD_Delivery_Date,POD_Delivery_Time,POD_Signature_Description,Actual_Weight_Amount,Actual_Weight_Units,Rated_Weight_Amount,Rated_Weight_Units,Number_of_Pieces,Service_Packaging,Shipper_Company,Shipper_Name,Shipper_Address_Line_1,Shipper_Address_Line_2,Shipper_City,Shipper_State,Shipper_Postal_Code,Shipper_Country_Territory,Recipient_Name,Recipient_Company,Recipient_Address_Line_1,Recipient_Address_Line_2,Recipient_City,Recipient_State,Recipient_Postal_Code,Recipient_Country_Territory,API_Pickup_Date,API_First_Activity_Date,API_First_Activity_Time, API Delivery Date,API Delivery Time,First Attempt Date, First Attempt Time, Standard Transit By Date,Standard Transit By Time,Status,Reason,Exception,Result_Header,Result_Notes X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208143,,7.54,0,Standard,,,,,,25.3,L,26,L,1,PKG,XXXXXXXXXX,,XXXXXXXXXXXX,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:02:00,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,, X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208144,,7.54,0,Standard,,,,,,25.2,L,26,L,1,PKG,,,,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:03:30,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,, X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208145,,7.25,0,Standard,,,,,,24.7,L,25,L,1,PKG,,,,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:02:33,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,, X1231Z1123,4/13/2019,0000A031,P/P,,1Z112091091208146,,4.64,0,Standard,,,,,,15,L,16,L,1,PKG,,,,,XXXXXX,XX,XXXXX,XX,XXXXXX,XXXXXXXXXXXXXXX,XXXXXXX,,XXX,XX,XXXXXX,XX,4/8/2019,4/8/2019,18:02:12,4/12/2019,14:57:58,,,4/12/2019,23:30:00,XX-XXX,,,,
Reply
#8
This seems to work, not sure if it the best way though Tongue
import pandas as pd
  
data = pd.read_csv("some_data.csv")
data.Result_Notes = data.Result_Notes.astype(str)
data.Result_Header = data.Result_Header.astype(str)
  
for index, series in data.iterrows():
    desired_number = series['Number_Header']
    data.at[index, 'Result_Header'] = desired_number
    data.at[index, 'Result_Notes'] = '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 ... Status Reason Exception Result_Header Result_Notes 0 X1231Z1123 4/13/2019 0000A031 P/P NaN ... XX-XXX NaN NaN 1Z112091091208143 This is your desired number 1 X1231Z1123 4/13/2019 0000A031 P/P NaN ... XX-XXX NaN NaN 1Z112091091208144 This is your desired number 2 X1231Z1123 4/13/2019 0000A031 P/P NaN ... XX-XXX NaN NaN 1Z112091091208145 This is your desired number 3 X1231Z1123 4/13/2019 0000A031 P/P NaN ... XX-XXX NaN NaN 1Z112091091208146 This is your desired number
Reply
#9
@Yoriz,

Thank you for your help! The script did run for me.

I have a question about a column header with spaces vs underscores. What would I do in a situation where I wanted to convert 'First Attempt Date' to astype(str)
Reply
#10
I think you would use data['First Attempt Date'] = data['First Attempt Date'].astype(str)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "list object has no attribute transpose()" usercat123 4 4,187 Jan-28-2022, 12:01 PM
Last Post: usercat123
  Python PDF merging from an excel pandas for loop siraero 0 2,165 Aug-16-2020, 09:34 AM
Last Post: siraero
  What is the mechanism of numpy function returning pandas object? Ibaraki 2 2,455 Apr-04-2020, 10:57 PM
Last Post: Ibaraki
  Python Pandas for loop/while loop question mrashy 1 3,836 Mar-24-2020, 04:39 AM
Last Post: deanhystad
  AttributeError: (“module 'pandas' has no attribute 'rolling_std'” Mariana136 4 7,558 Sep-23-2019, 12:56 PM
Last Post: Mariana136
  Loop pandas data frame by position ? Johnse 1 2,245 Sep-06-2019, 12:26 AM
Last Post: scidam
  AttributeError: module 'numpy' has no attribute 'array aapurdel 7 45,276 May-29-2019, 02:48 AM
Last Post: heiner55
  AttributeError: 'NoneType' object has no attribute 'all' synthex 2 5,207 Mar-07-2019, 11:11 AM
Last Post: synthex
  Please help with AttributeError: 'Netz' object has no attribute 'conv' DerBerliner 2 3,723 Feb-27-2019, 06:01 PM
Last Post: DerBerliner
  'list' object has no attribute 'reshape' SamSoftwareLtd 1 15,482 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