python - How can I round values in Pandas DataFrame containing mixed datatypes for further data comparison? -
i have dataframe df_left:
idx1 idx2 idx3 idx4 valuetype value 0 a1 q 1983 q4 w 10.123 1 a1 q 1983 q4 x 2 a1 q 1983 q4 y f 3 a1 q 1983 q4 z nan 4 a1 q 1984 q1 w 110.456 ...
created previous post:
and dataframe df_right:
idx1 idx2 idx3 idx4 valuetype value 0 a1 q 1983 q4 w 10 1 a1 q 1983 q4 x 2 a1 q 1983 q4 y f 3 a1 q 1983 q4 z nan 4 a1 q 1984 q1 w 110
i compare , reconcile data both values , text of following works:
df_compare = pd.merge(df_left, df_right, how ='outer', on = ['idx1', 'idx2', 'idx3', 'idx4', 'valuetype']) df_compare.columns = ['idx1', 'idx2', 'idx3', 'idx4', 'valuetype', 'from', 'to'] df_compare = df_compare[df_compare.from!=df_compare.to]
whilst results expected, before comparison round data in value coulmn.
i have tried:
df.apply(np.round)
and also:
df.round(decimals=0, out=none)
but both expected thow error:
attributeerror: ("'str' object has no attribute 'rint'", u'occurred @ index code')
a custom method rounding floats may solve rounding mixed dtype column
in [238]: def round_float(s): '''1. if s float, round 0 decimals 2. else return s ''' import re m = re.match("(\d+\.\d+)",s.__str__()) try: r = round(float(m.groups(0)[0]),0) except: r = s return r in [239]: s = u''' idx1 idx2 idx3 idx4 valuetype value 0 a1 q 1983 q4 w 10.23 1 a1 q 1983 q4 x 2 a1 q 1983 q4 y f 3 a1 q 1983 q4 z nan 4 a1 q 1984 q1 w 110.15''' in [240]: df1 = pd.read_csv(stringio(s), delimiter="\s+") in [241]: df1["value2"] = df1.value.apply(round_float) in [242]: df1 out[242]: idx1 idx2 idx3 idx4 valuetype value value2 0 a1 q 1983 q4 w 10.23 10 1 a1 q 1983 q4 x 2 a1 q 1983 q4 y f f 3 a1 q 1983 q4 z nan nan 4 a1 q 1984 q1 w 110.15 110
Comments
Post a Comment