lesley2958
lesley2958

Reputation: 2608

Delete `dtype: object` in pandas DataFrame print

Here's the code I have:

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

This returns:

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

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.

Upvotes: 1

Views: 6096

Answers (2)

Grr
Grr

Reputation: 16079

That isn't a row. It's a description of the data you are printing. Try printing just the .values and you will see.

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

Update Based on your comment:

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

Upvotes: 3

piRSquared
piRSquared

Reputation: 294358

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

Try:

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

Or I'd rewrite your function

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)

If you want a string

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]

Upvotes: 3

Related Questions