【发布时间】:2022-06-10 20:53:54
【问题描述】:
我尝试获取一个字典,其中包含逗号后两位数字的值,主要用于 0.0 作为 0.00。有什么建议我怎么能得到那个?我试图迭代抛出字典并将 0.0 替换为 0.00 但我无法解决它
import pandas as pd
from tkinter import *
def create():
data = {'1': [0.99999, 0.00000, 1.22222, 0.000000], '2': [0.99999, 0.00000, 1.22222, 0.000000]}
df = pd.DataFrame(data)
df2 = df.round(decimals = 2)
print(df2)
my_dictionary = df2.to_dict()
print(my_dictionary)
window = Tk()
window.geometry("200x200")
button = Button(text="Convert",command=create)
button.pack()
window.mainloop()
输出
df2:
1 2
0 1.00 1.00
1 0.00 0.00
2 1.22 1.22
3 0.00 0.00
我的字典:
{'1': {0: 1.0, 1: 0.0, 2: 1.22, 3: 0.0}, '2': {0: 1.0, 1: 0.0, 2: 1.22, 3: 0.0}}
我想要:
{'1': {0: 1.0, 1: 0.00, 2: 1.22, 3: 0.00}, '2': {0: 1.0, 1: 0.00, 2: 1.22, 3: 0.00}}
【问题讨论】:
-
如果你有真正的浮动,那是不可能的,你不能(也不应该)控制表示,也许你需要字符串?
标签: python pandas dataframe dictionary