print small DataFrame to STDOUT and read it back into dataframe

Luca please_no at spam.it
Tue Apr 7 09:05:18 EDT 2020


On 4/6/2020 11:05 PM, Luca wrote:
> On 4/6/2020 8:51 PM, Reto wrote:
>> out = df.to_csv(None)
>> new = pd.read_csv(io.StringIO(out), index_col=0)
> 
> Thank you, brother. It works
> 

BTW, a little gotcha (I write this in case someone gets here in the 
future through Google or something)

"""
import pandas as pd
import numpy as np
import io
df = pd.DataFrame(10*np.random.randn(3,4))
df = df.astype(int)
out = df.to_csv(None)

# out == ',0,1,2,3\n0,9,4,-5,-2\n1,16,12,-1,-5\n2,-2,8,0,6\n'

new = pd.read_csv(io.StringIO(out), index_col=0)

#gotcha
type(df.iloc[1,1]) # numpy.int32
type(new.iloc[1,1]) # numpy.int64
"""

new == out will return a dataframe of False

         0	1	2	3
0	False	False	False	False
1	False	False	False	False
2	False	False	False	False

Thanks again


More information about the Python-list mailing list