【问题标题】:Delete `dtype: object` in pandas DataFrame print删除 pandas DataFrame 打印中的 `dtype: object`
【发布时间】:2017-04-17 21:18:45
【问题描述】:

这是我的代码:

def boba_recs(lat, lng):
    f1 = pd.read_csv("./boba_final.csv") 
    user_loc = Point(lng, lat) # converts user lat/long to point object
    # makes dataframe of distances between each boba place and the user loc
    f1['Distance'] = [user_loc.distance(Point(xy)) for xy in zip(f1.Longitude, f1.Lat)]
    # grabs the three smallest distances
    boba = f1.nsmallest(3, 'Distance').set_index('Name') # sets index to name
    return(": " + boba['Address'])

这会返回:

Name
Coco Bubble Tea            : 129 E 45th St New York, NY 10017
Gong Cha                   : 75 W 38th St, New York, NY 10018
Smoocha Tea & Juice Bar      : 315 5th Ave New York, NY 10016
Name: Address, dtype: object

几乎完美,除了我想摆脱“名称:地址,dtype:对象”行。我已经尝试了一些东西,但它不会在不弄乱其他所有东西的格式的情况下消失。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    'Address'pd.Series 的名称,'Name' 是索引的名称

    试试:

    s.rename_axis(None).rename(None)
    
    Coco Bubble Tea            : 129 E 45th St New York, NY 10017
    Gong Cha                   : 75 W 38th St, New York, NY 10018
    Smoocha Tea & Juice Bar      : 315 5th Ave New York, NY 10016
    dtype: object
    

    或者我会重写你的函数

    def boba_recs(lat, lng):
        f1 = pd.read_csv("./boba_final.csv") 
        user_loc = Point(lng, lat) # converts user lat/long to point object
        # makes dataframe of distances between each boba place and the user loc
        f1['Distance'] = [user_loc.distance(Point(xy)) for xy in zip(f1.Longitude, f1.Lat)]
        # grabs the three smallest distances
        boba = f1.nsmallest(3, 'Distance').set_index('Name') # sets index to name
        return(": " + boba['Address']).rename_axis(None).rename(None)
    

    如果你想要一个字符串

    def boba_recs(lat, lng):
        f1 = pd.read_csv("./boba_final.csv") 
        user_loc = Point(lng, lat) # converts user lat/long to point object
        # makes dataframe of distances between each boba place and the user loc
        f1['Distance'] = [user_loc.distance(Point(xy)) for xy in zip(f1.Longitude, f1.Lat)]
        # grabs the three smallest distances
        boba = f1.nsmallest(3, 'Distance').set_index('Name') # sets index to name
        temp = (": " + boba['Address']).rename_axis(None).__repr__()
        return temp.rsplit('\n', 1)[0]
    

    【讨论】:

    • 是的,我试过了,但它仍然有 dtype: object 部分。
    • 当您返回 pd.Series 时,它会在您的屏幕上显示从 __repr__ 方法返回的字符串。该方法被编写为在末尾返回dtype 作为描述的一部分。我的问题是,你想返回一个格式正确的字符串吗?或者你想返回一个熊猫系列对象?如果你想要 pandas 系列对象,那么你将不得不在描述中使用 dtype
    • 格式正确的字符串,不一定是系列
    • @user3611375 如果这能解决您的问题,请不要忘记接受。
    【解决方案2】:

    这不是一行。这是对您正在打印的数据的描述。尝试只打印.values,您会看到。

    [Coco Bubble Tea            : 129 E 45th St New York, NY 10017
    Gong Cha                   : 75 W 38th St, New York, NY 10018
    Smoocha Tea & Juice Bar      : 315 5th Ave New York, NY 10016]
    

    更新根据您的评论:

    print(pd.DataFrame(your_series))
    Name
    Coco Bubble Tea            : 129 E 45th St New York, NY 10017
    Gong Cha                   : 75 W 38th St, New York, NY 10018
    Smoocha Tea & Juice Bar      : 315 5th Ave New York, NY 10016
    

    【讨论】:

    • 同样,并不完全理想,因为那样它将是一个列表而不是返回字符串。当然,我可以遍历它,但是如果我可以做些什么来直接从数据框中格式化它,我非常喜欢它。
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    相关资源
    最近更新 更多