Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-07 Thread Luca

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
--
https://mail.python.org/mailman/listinfo/python-list


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Luca

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

--
https://mail.python.org/mailman/listinfo/python-list


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Reto
On Mon, Apr 06, 2020 at 06:29:01PM -0400, Luca wrote:
> so, given a dataframe, how do I make it print itself out as CSV?

read the docs of to_csv...

> And given CSV data in my clipboard, how do I paste it into a Jupiter cell
> (possibly along with a line or two of code) that will create a dataframe out
> of it?

```python
import pandas as pd
import io
df = pd.DataFrame(data=range(10))
out = df.to_csv(None)
new = pd.read_csv(io.StringIO(out), index_col=0)
```

That'll do the trick... any other serialization format works similarly.
you can copy out to wherever, it's just csv data.

Cheers,
Reto
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Luca

On 4/6/2020 3:03 PM, Christian Gollwitzer wrote:





CSV is the most sensible option here. It is widely supported by 
spreadsheets etc. and easily copy/pasteable.


Thank you Christian.

so, given a dataframe, how do I make it print itself out as CSV?

And given CSV data in my clipboard, how do I paste it into a Jupiter 
cell (possibly along with a line or two of code) that will create a 
dataframe out of it?



--
https://mail.python.org/mailman/listinfo/python-list


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Christian Gollwitzer

Am 06.04.20 um 17:17 schrieb Luca:

On 4/6/2020 4:08 AM, Reto wrote:

Does this help?


Thank you, but not really. What I am trying to achieve is to have a way 
to copy and paste small yet complete dataframes (which may be the result 
of previous calculations) between a document (TXT, Word, GoogleDoc) and 
Jupiter/IPython.


Did I make sense?



CSV is the most sensible option here. It is widely supported by 
spreadsheets etc. and easily copy/pasteable.


Christian
--
https://mail.python.org/mailman/listinfo/python-list


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Luca

On 4/6/2020 4:08 AM, Reto wrote:

Does this help?


Thank you, but not really. What I am trying to achieve is to have a way 
to copy and paste small yet complete dataframes (which may be the result 
of previous calculations) between a document (TXT, Word, GoogleDoc) and 
Jupiter/IPython.


Did I make sense?

Thanks
--
https://mail.python.org/mailman/listinfo/python-list


Re: print small DataFrame to STDOUT and read it back into dataframe

2020-04-06 Thread Reto
On Sat, Apr 04, 2020 at 07:00:23PM -0400, Luca wrote:
> dframe.to_string
> 
> gives:
> 
>  0  a0  b0  c0  d0
> 1  a1  b1  c1  d1
> 2  a2  b2  c2  d2
> 3  a3  b3  c3  d3>

That's not the output of to_string.
to_string is a method, not an attribute which is apparent by the

> 

comment in your output.
You need to call it with parenthesis like `dframe.to_string()`

> Can I evaluate this string to obtain a new dataframe like the one that
> generated it?

As for re-importing, serialize the frame to something sensible first.
There are several options available, csv, json, html... Take your pick.

You can find all those in the dframe.to_$something namespace
(again, those are methods, make sure to call them).

Import it again with pandas.read_$something, choosing the same serialization 
format
you picked for the output in the first place.

Does this help?

Cheers,
Reto
-- 
https://mail.python.org/mailman/listinfo/python-list


print small DataFrame to STDOUT and read it back into dataframe

2020-04-04 Thread Luca



possibly a stupid question. Let's say I have a (small) dataframe:

import pandas as pd
dframe = pd.DataFrame({'A': ['a0','a1','a2','a3'],
'B': ['b0','b1','b2','b3'],
'C': ['c0','c1','c2','c3'],
'D': ['d0','d1','d2','d3']}, index=[0,1,2,3])

Is there a way that I can ask this dataframe to "print itself" in a way 
that I can copy that output and easily rebuild the original dataframe 
with index, columns and all?


dframe.to_string

gives:



Can I evaluate this string to obtain a new dataframe like the one that 
generated it?


Thanks
--
https://mail.python.org/mailman/listinfo/python-list