我有一些看起来像下面的数据
data1 = [[(271.760309837,)], [(289.247745329,)]]
data2 = [(u'A', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 2.66666666666667), (u'B', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.66666666666667), (u'C', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 12.25), (u'D', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 5.875), (u'E', datetime.datetime(2019, 8, 23, 0, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 9.06451612903226), (u'F', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 5.363636363636), (u'G', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.5), (u'H', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 9.53125), (u'I', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.2), (u'J', datetime.datetime(2019, 8, 23, 1, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 7.0967741354839), (u'K', datetime.datetime(2019, 8, 23, 2, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.25), (u'L', datetime.datetime(2019, 8, 23, 2, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 5.1153846153846), (u'M', datetime.datetime(2019, 8, 23, 2, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 6.387096419355), (u'N', datetime.datetime(2019, 8, 23, 2, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 4.47058529412), (u'O', datetime.datetime(2019, 8, 23, 2, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 3.727272727273), (u'P', datetime.datetime(2019, 8, 23, 3, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=0, name=None)), 5.2)]
data3 = [[('A', 204.593564568), ('B', 217.421341061), ('C', 237.296250326), ('D', 217.464281998), ('E', 206.329901299)], [('F', 210.297625953), ('G', 228.117692718), ('H', 4), ('I', 265.319671257), ('K',)]]
如您所见,浮点值在小数点后有两位数字。我想使浮点值出现在小数点后一位。在线查找答案时,我遇到的最常见建议是使用round()
函数。所以我尝试了
import pandas as pd
import datetime
import psycopg2
df = pd.DataFrame(data1).round(2).values.tolist()
df = pd.DataFrame(data2).round(2).values.tolist()
df = pd.DataFrame(data3).round(2).values.tolist()
但是它仅适用于data2
,而其他两个数据没有显示四舍五入。
此外,我还尝试使用numpy
来获得结果
df = pd.Dataframe(data1)
np.round(df, decimals=2).values
df = pd.Dataframe(data2)
np.round(df, decimals=2).values
df = pd.Dataframe(data2)
np.round(df, decimals=2).values
但它再次仅适用于data2
。如何确保大熊猫中任何数据格式的四舍五入或将其限制为两位小数?
答案 0 :(得分:4)
问题在于数据格式,有元组而不是标量。因此,可能的解决方案是将DataFrame.applymap
用于元素明智的应用lambda函数-可能存在圆形浮点数:
f = lambda x: tuple([round(y, 2) if isinstance(y, float) else y for y in x])
df = pd.DataFrame(data1).applymap(f).values.tolist()
print (df)
[[(271.76,)], [(289.25,)]]
df = pd.DataFrame(data3).applymap(f).values.tolist()
print (df)
[[('A', 204.59), ('B', 217.42), ('C', 237.3), ('D', 217.46),
('E', 206.33)], [('F', 210.3), ('G', 228.12), ('H', 4), ('I', 265.32), ('K',)]]