我正在从如下列表中的字典创建数据框:
[{'points': [-9.3613566605198262, 4.0910018605047171, -9.3473134841172731, 4.0960021770649475], 'uid': 0},
{'points': [-9.4687215264026712, 4.0132947848279499, -9.3641925245477857, 4.0159059337303784], 'uid': 1},
上面是一个叫做l1的列表
对于数据框:
df = pd.DataFrame.from_records(l1)
这将创建以下 df:
points uid
0 [-9.3613566605198262, 4.09100186050......] 0
1 [-9.4687215264026712, 4.01329478482......] 1
我想知道如何从点列中删除列表格式/方括号,它应该包含简单的逗号分隔值。
另外,如何限制小数点后两位数?
我尝试了以下方法,但它不起作用。
np.round(df, 2)
和
df.round({'points':2})
预期的 df
points uid
0 -9.36, 4.09, -9.34, 4.09 0
1 -9.46, 4.01, -9.36, 4.01 1
最佳答案
使用 apply 将列表中的数字四舍五入,然后拼接成一个字符串。
df['points'] = df.apply(lambda x: ', '.join(str(round(e,2)) for e in x.points),axis=1)
df
Out[1042]:
points uid
0 -9.36, 4.09, -9.35, 4.1 0
1 -9.47, 4.01, -9.36, 4.02 1
关于python - Pandas 数据框列的值作为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448102/