Skip to content
Advertisement

Is there the equivalent of to_markdown to read data?

With pandas 1.0.0 the use of .to_markdown() to show the content of a dataframe in this forum in markdown is going to proliferate. Is there a convenient way to load the data back into a dataframe? Maybe an option to .from_clipboard(markdown=True)?

Advertisement

Answer

None of the answers so far read the data from the clipboard. They all require the data to be in a string. This led me to look into the source code of pandas.read_clipboard(), and lo and behold, the method internally uses pandas.read_csv(), and passes all arguments to it. This automatically leads to the following solution:

t = pd.DataFrame({"a": [0, 1], "b":[2, 3]}).to_markdown()                                                                                                                                                    
print(t)
|    |   a |   b |
|---:|----:|----:|
|  0 |   0 |   2 |
|  1 |   1 |   3 |

Mark the table and copy it to the clipboard (Ctrl-C on Windows). Building on the above answers:

pd.read_clipboard(sep="|", header=0, index_col=1, skipinitialspace=True).dropna(
    axis=1, how="all"
).iloc[1:]

The next step would be to integrate this into https://pyjanitor-devs.github.io/pyjanitor/ so it can be easily called with a markdown=True parameter.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement