【问题标题】:how to print data of a specific user using .loc function in dataframe with the help of index如何借助索引在数据框中使用 .loc 函数打印特定用户的数据
【发布时间】:2020-10-06 17:47:59
【问题描述】:

我已经创建了一个数据框

tmp = pd.DataFrame.from_dict(all_pred)
tmp_transpose = tmp.transpose()

tmp_transpose.head(3)

然后我将索引标记为 userId

tmp_transpose = tmp_transpose.rename_axis('userId')
tmp_transpose

我想获取一个用户的数据,因此出现错误

result=tmp_transpose.loc[196] 

【问题讨论】:

  • 始终将代码、数据和错误消息以文本形式而非图像形式呈现

标签: python pandas numpy dataframe series


【解决方案1】:

您的索引似乎是字符串"196",而不是数字196

result = tmp_transpose.loc["196"]

最小的工作示例

import pandas as pd

#tmp = pd.DataFrame({'192': [1,2,3], '196': [1,2,3]})
tmp = pd.DataFrame.from_dict({'192': [1,2,3], '196': [1,2,3]})
tmp_transpose = tmp.transpose()

#print(tmp_transpose.head(3))

tmp_transpose = tmp_transpose.rename_axis('userId')
#print(tmp_transpose)

print(tmp_transpose.loc['196'])  # OK
print(tmp_transpose.loc[196])    # error

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多