I am trying to format the rows of my data frame, without success.
My data frame output is:
X Y
443 cd2a9dd781c1396d4000ae05fcc3a0b00a5dc4f9 889.825
111 ae3faf7ed08967e93d5f5ed6e10a5b256ec8c7fa 883.275
221 601f669c760687b84ec57fe1eec213e26114a262 868.345
631 80f54ce2aa2839e80cd5447cb369ec31f5e1fd47 867.545
I would like the output in the following format:
{"X": "cd2a9dd781c1396d4000ae05fcc3a0b00a5dc4f9", "Y": 889.8}
{"X": "ae3faf7ed08967e93d5f5ed6e10a5b256ec8c7fa", "Y": 883.2}
I tried with:
format = '{"X": "{}", "Y": {%.1f}}'.format
my_df.apply(lambda x: format(**x), 1)
and:
my_df.style.format({'X': '{"X": "{}",', 'Y': '"Y": {%.1f}}'})
and:
my_df.to_string(formatters={'X':'"X": "{}",'.format, 'Y':'"Y": {%.1f}'.format})
None worked for me. This last attempt (to_string
) is returning the following error:
File "/usr/local/lib/python3.5/dist-packages/pandas/io/formats/format.py", line 1781, in get_result
fmt_values = self._format_strings()
File "/usr/local/lib/python3.5/dist-packages/pandas/io/formats/format.py", line 1961, in _format_strings
return [self.formatter(x) for x in self.values]
File "/usr/local/lib/python3.5/dist-packages/pandas/io/formats/format.py", line 1961, in
return [self.formatter(x) for x in self.values]
KeyError: '%'
Any suggestion/help?