python - 删除 pandas DataFrame 打印中的 `dtype: object`

标签 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

几乎完美,只是我想去掉“Name: Address, dtype: object”行。我已经尝试了一些东西,它不会在不弄乱其他所有内容的格式的情况下消失。

最佳答案

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

关于python - 删除 pandas DataFrame 打印中的 `dtype: object`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43459794/

相关文章:

python - 将 Pandas 'categorical' dtype 与 sklearn 一起使用

python - 根据条件就地修改列

python - 我想根据其他 Dataframe 中的匹配值填充 Dataframe 中的值

r - 我正在比较两列以创建第三列...它不起作用?

python - 如何使用 urllib2 将经过身份验证的代理异常应用于开启器?

python - 如何修复 'in bash after checking with if condition else statements also getting executed'?

python - ValueError : Image is not numeric, 但 ndarray

python - 在 pandas 数据框中的 block 中查找第一个 'True' 值

python - 索引错误 : index 1491188345 is out of bounds for axis 0 with size 1491089723

r - 如何计算R中的加权和?