Issue
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)
?
Solution
You can read markdown tables (or any structured text table) with the pandas read_table
function:
Let's create a sample markdown table:
pd.DataFrame({"a": [0, 1], "b":[2, 3]}).to_markdown()
| | a | b |
|---:|----:|----:|
| 0 | 0 | 2 |
| 1 | 1 | 3 |
As you can see, this is just a structured text table where the delimiters are pipes, there's a lot of whitespace, there are null columns on the left-most and right-most, and there's a header underline that must be dropped.
pd
# Read a markdown file, getting the header from the first row and inex from the second column
.read_table('df.md', sep="|", header=0, index_col=1, skipinitialspace=True)
# Drop the left-most and right-most null columns
.dropna(axis=1, how='all')
# Drop the header underline row
.iloc[1:]
a b
0 0 2
1 1 3
Answered By - Dave
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.