0

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?

Dalton Cézane
  • 3,672
  • 2
  • 35
  • 60

2 Answers2

1

For me working last solution with change % to ::

df = df.to_string(formatters={'X':'"X": "{}",'.format, 'Y':'"Y": {:.1f}'.format})
print (df)
                                                    X          Y
443  "X": "cd2a9dd781c1396d4000ae05fcc3a0b00a5dc4f9", "Y": 889.8
111  "X": "ae3faf7ed08967e93d5f5ed6e10a5b256ec8c7fa", "Y": 883.3
221  "X": "601f669c760687b84ec57fe1eec213e26114a262", "Y": 868.3
631  "X": "80f54ce2aa2839e80cd5447cb369ec31f5e1fd47", "Y": 867.5

If need new formatting column is possible use list comprehension:

df['new'] = ['"X": "{}", "Y": {:.1f}'.format(i, j) for i, j in zip(df['X'], df['Y'])]
print (df)
                                            X        Y  \
443  cd2a9dd781c1396d4000ae05fcc3a0b00a5dc4f9  889.825   
111  ae3faf7ed08967e93d5f5ed6e10a5b256ec8c7fa  883.275   
221  601f669c760687b84ec57fe1eec213e26114a262  868.345   
631  80f54ce2aa2839e80cd5447cb369ec31f5e1fd47  867.545   

                                                   new  
443  "X": "cd2a9dd781c1396d4000ae05fcc3a0b00a5dc4f9...  
111  "X": "ae3faf7ed08967e93d5f5ed6e10a5b256ec8c7fa...  
221  "X": "601f669c760687b84ec57fe1eec213e26114a262...  
631  "X": "80f54ce2aa2839e80cd5447cb369ec31f5e1fd47...  
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • It worked. But the X values are being printed as "cd2a9dd781c1396d4000ae05fcc3a...", which is incomplete. What can I do in order to output the complete string? "cd2a9dd781c1396d4000ae05fcc3a0b00a5dc4f9" instead of "cd2a9dd781c1396d4000ae05fcc3a..."? Thank you. – Dalton Cézane Jun 14 '18 at 03:56
  • 1
    @Dalton Cézane it is display problem and solution is [here](https://stackoverflow.com/q/29902714/2901002) – jezrael Jun 14 '18 at 04:41
0

try

my_df.to_dict(orient='records')
nimrodz
  • 1,504
  • 1
  • 13
  • 18