在 pandas DataFrame打印中删除`dtype:object` [英] Delete `dtype: object` in pandas DataFrame print

查看:507
本文介绍了在 pandas DataFrame打印中删除`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'])

这将返回:

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:对象"行.我已经尝试了几件事,而且不会弄乱其他所有内容的格式就不会消失.

Almost perfect except I want to get rid of the "Name: Address, dtype: object" row. I've tried a few things and it just won't go away without messing up the format of everything else.

推荐答案

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

'Address' is the name of the pd.Series and 'Name' is the name of the index

尝试:

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]

这篇关于在 pandas DataFrame打印中删除`dtype:object`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆