TypeError:pandasq中应为字符串或类似字节的对象

2024-06-02 07:47:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python3.x中运行pandasql,我有一个客户城市数据。样本如下:

我想按城市检索不同的客户号码。所以我用了下面的代码

import io
import pandas as pd
import os 
import pandasql as pdsql
os.chdir(path) 

df = pd.read_csv(StringIO(
"""CUSTOMER_ID, City
21397845, Birmingham
26396841, Anchorage
52396841, Bullhead
67896841, Flagstaff"""))

def Cust(city_value):


    city='"'+city_value+'"'
    print("city="+city)

    str2='select * from df_city where City='+ city
    pysql = lambda q: pdsql.sqldf(q, globals())
    df1 = pysql(str2)
    return df1

东吴市是一个全球性的变量。但是运行此查询后,我收到以下错误消息

^{pr2}$

你能帮我修改一下代码吗?在


Tags: 代码importcitydf客户valueosas
1条回答
网友
1楼 · 发布于 2024-06-02 07:47:07

你的Python 7.2不能复制我的问题

import StringIO
import pandas as pd
import pandasql as pdsql 

df = pd.read_csv(StringIO(
"""CUSTOMER_ID,City
21397845,Birmingham
26396841,Anchorage
52396841,Bullhead
67896841,Flagstaff"""))

def Cust(city_value):
    city='"'+city_value+'"'
    print("city="+city)
    str2='select * from df where City='+ city
    pysql = lambda q: pdsql.sqldf(q, globals())
    df1 = pysql(str2)
    return df1

Cust('Birmingham')

退货:

^{pr2}$

[注意:我删除了df数据中的空白,与你的帖子相比(我的编辑,下次再发。。。:-/)

相关问题 更多 >