Let's assume I have a Pandas's DataFrame
:
import numpy as np
import pandas as pd
df = pd.DataFrame(
np.random.randint(0, 100, size=(10, 4)), columns=('A', 'DA', 'B', 'DB'))
which outputs:
A DA B DB
0 62 87 10 39
1 56 7 81 12
2 37 26 21 44
3 56 26 42 32
4 29 45 11 9
5 11 85 4 79
6 87 31 61 90
7 5 55 26 47
8 55 94 20 84
9 52 26 72 19
I would like to convert that to this:
A B
0 62±87 10±39
1 56±7 81±12
2 37±26 21±44
3 56±26 42±32
4 29±45 11±9
5 11±85 4±79
6 87±31 61±90
7 5±55 26±47
8 55±94 20±84
9 52±26 72±19
and viceversa.
I could do this "by hand" but I was hoping for an elegant way using Pandas' built-ins, which could eventually be converted elegantly to LaTeX (i.e. 62±87
becomes $62 \pm 87$
).
I was looking into .apply()
from Converting a column within pandas dataframe from int to string but it is not clear to me how to use it for this purpose.
EDIT
The suggested answer do NOT seem to cover the VICEVERSA: i.e. convertin from the A±DA
notation back to two columns A
and DA
.