在pandas DataFrame print中删除`dtype:object`

时间:2017-04-17 21:18:45

标签: python pandas dataframe

这是我的代码:

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:object"行。我已经尝试了一些东西,它只是在没有弄乱其他一切的格式的情况下不会消失。

2 个答案:

答案 0 :(得分:3)

这不是一排。它是您正在打印的数据的描述。尝试仅打印.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

答案 1 :(得分:3)

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