I have a short Python script that uses pandas to read an Excel file and then create a SQL INSERT command.
Inside the script, I need to replace certain character strings.
However, when I do, I get this error:
AttributeError: 'Pandas' object has no attribute 'replace'
Here is my script:
import pandas as pd
df = pd.read_excel('JulyData.xlsx')
# print(df)
# print(df.iloc[0, 0])
print('INSERT INTO project(name, object, amount, value)')
for row in df.itertuples(index=False):
rowString = row
rowString = rowString.replace(' " ', " ")
rowString = rowString.replace(' – ', " ")
rowString = rowString.replace(' / ', " & ")
rowString = rowString.replace(' ’ ', " ")
print(f'VALUES {tuple(rowString)}')
print(f'WAITFOR DELAY \'00:00:02\'')
print('\n')
Is there a way to do this in pandas?
Thanks!
sample output:
{'name': ['Xu–, Yi', 'Gare, /Mark'], 'object': ['xuy@anes’.mty.edu', '"[email protected]'], 'amount': ['100', '200'], 'value': ['"abc"', 'def']}
Pandasis the name of the namedtuplerow, is has no method replace. You should usereplacereplace? Or am I not using it correctly? Thanks!replacemethod of pandas, not a non-existingreplacemethod of a nametuple. If you can share a short example (output ofdf.head().to_dict('list')) I can post an answer.