如何使用大熊猫替换Excel数据中的某些值?

发布于 2025-02-13 16:16:32 字数 1120 浏览 0 评论 0原文

我有一个简短的python脚本,它使用pandas读取Excel文件,然后创建sql insert命令。

在脚本中,我需要替换某些字符字符串。

但是,当我这样做时,我会收到此错误:

AttributeError: 'Pandas' object has no attribute 'replace'

这是我的脚本:

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')

有没有办法在pandas中做到这一点?

谢谢!

样本输出:

{'name': ['Xu–, Yi', 'Gare, /Mark'], 'object': ['xuy@anes’.mty.edu', '"[email protected]'], 'amount': ['100', '200'], 'value': ['"abc"', 'def']}

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']}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开雨落又逢春i 2025-02-20 16:16:32

pandas是名为Tuple的名称ROWInternows返回,并且名为Tupuple当然没有方法replace>替换。您需要的是PANDAS方法 (对于整个数据框架)或字符串评估器的单个列)。

示例:

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)

结果:

          name            object amount value
0       Xu, Yi  [email protected]    100   abc
1  Gare, &Mark     [email protected]    200   def

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文