Reputation: 3038
I have some variety of data which looks like below
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',)]]
As you can see, the float values have couple of digits after decimal. I would like to make any occurrence of float value to a one digit after decimal. Looking up for answers online, the most common suggestion I came across was to use the round()
function. So I tried this
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()
But it only worked for data2
while the other two data didn't show any rounding.
Furthermore, I also tried using numpy
to achieve the result
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
But again it only worked for data2
. How can I ensure the rounding or limiting it to two decimal places is consistent across any data formats in pandas?
Upvotes: 2
Views: 1291
Reputation: 863741
Problem is in format of data, there are tuples instead scalars. So possible solution is use DataFrame.applymap
for element wise apply lambda function - there is possible round floats:
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',)]]
Upvotes: 5