In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'], index=['foo', 'bar'])
In [4]: df
Out[4]:
A B
foo 1 2
bar 3 4
In [5]: df.append(pd.Series(np.random.rand(2), index=['A', 'B'], name='baz'))
Out[5]:
A B
foo 1.000000 2.000000
bar 3.000000 4.000000
baz 0.190326 0.332797
Problem description
My use case was adding a row that is the result of bar/foo
to represent it as a percentage.
Expected Output
Out[5]:
A B
foo 1 2
bar 3 4
baz 0.190326 0.332797
Comment From: TomAugspurger
That's how pandas works, the dtypes are per-column, so appending floats changes everything.
I'd recommend writing a helper function for display that truncates the string representation down as far as possible.