英文:
UPDATE function in sqldf in Python
问题
在Python平台中使用'from pandasql import sqldf'构建SQL代码。执行下面的代码,结果输出为"没有这样的表df"。有人能够纠正吗?
query = """
UPDATE df
SET C = C * 10
WHERE C < 50
"""
英文:
Building SQL code in Python platform using 'from pandasql import sqldf'.
Executed code below and it turned out to be "no such table df" as output.
Anyone able to rectify it?
query="""
UPDATE df
SET C = C*10
WHERE C <50
"""
答案1
得分: 1
pandasql不支持更新操作,您需要使用类似以下方式:
from pandasql import sqldf
import pandas as pd
df = pd.DataFrame({
'A': ['foo', 'bar', 'baz', 'foo', 'bar', 'baz'],
'B': ['one', 'one', 'two', 'three', 'two', 'two'],
'C': [10, 20, 30, 40, 50, 60],
'D': [7, 8, 9, 10, 11, 12]
})
query = """
SELECT *
FROM df
WHERE column1 > 10;
"""
result = sqldf(query, globals())
result.loc[result['C'] < 50, 'C'] *= 10
英文:
pandasql does not support update operations, you need to use something like this:
from pandasql import sqldf
import pandas as pd
df = pd.DataFrame({
'A': ['foo', 'bar', 'baz', 'foo', 'bar', 'baz'],
'B': ['one', 'one', 'two', 'three', 'two', 'two'],
'C': [10, 20, 30, 40, 50, 60],
'D': [7, 8, 9, 10, 11, 12]
})
query = """
SELECT *
FROM df
WHERE column1 > 10;
"""
result = sqldf(query, globals())
result.loc[result['C'] < 50, 'C'] *= 10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论