1

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']}
4
  • Pandas is the name of the namedtuple row, is has no method replace. You should use replace Commented Jul 7, 2022 at 15:47
  • @Stef but I am using replace? Or am I not using it correctly? Thanks! Commented Jul 7, 2022 at 15:48
  • You need to use the replace method of pandas, not a non-existing replace method of a nametuple. If you can share a short example (output of df.head().to_dict('list')) I can post an answer. Commented Jul 7, 2022 at 15:58
  • @Stef ok thanks! I edited my question and included short sample at the end. Thank you Commented Jul 7, 2022 at 16:12

1 Answer 1

1

Pandas is the name of the namedtuple row returned by interrows, and a namedtuple of course has no method replace. What you need is the pandas method replace (for the whole data frame) or the string assessor's replace (for individual columns).

Example:

df = pd.DataFrame({'name': ['Xu–, Yi', 'Gare, /Mark'], 'object': ['xuy@anes’.mty.edu', '"[email protected]'], 'amount': ['100', '200'], 'value': ['"abc"', 'def']})
#          name               object amount  value
#0    Xu–, Yi  xuy@anes’.mty.edu    100  "abc"
#1  Gare, /Mark       "[email protected]    200    def

df.replace(['"',  '–', '/', '’'],
           ['',  '', '&', '' ],
           regex=True)

Result:

          name            object amount value
0       Xu, Yi  [email protected]    100   abc
1  Gare, &Mark     [email protected]    200   def
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, what are all the \1 and \2 for? thanks again
These are placeholders for matched parts, you don't need them, sorry - my bad. I've updated the answer with your data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.