Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index=False option to_markdown() #32667

Closed
chrisjcameron opened this issue Mar 12, 2020 · 9 comments · Fixed by #33091
Closed

Add index=False option to_markdown() #32667

chrisjcameron opened this issue Mar 12, 2020 · 9 comments · Fixed by #33091
Assignees
Labels
Enhancement IO Data IO issues that don't fit into a more specific label
Milestone

Comments

@chrisjcameron
Copy link

Problem description

This is a feature request to add an optional index=False argument to the new to_markdown() dataframe method. This would produce a markdown table without the initial unnamed index column.

@dsaxton
Copy link
Member

dsaxton commented Mar 12, 2020

This is actually possible by passing showindex=False, but could probably be better documented (it's one of the tabulate kwargs):

In [1]: import pandas as pd       
                                                                                                                                                                                                        
In [2]: df = pd.DataFrame({"a": [1, 2, 3]})              
                                                                                                                                                                                 
In [3]: print(df.to_markdown(showindex=False))                                                                                                                                                                                            
|   a |
|----:|
|   1 |
|   2 |
|   3 |

@MarcoGorelli
Copy link
Member

TBH I'm not sure it's worth documenting tabulate-specific kwargs, but adding an example showing the behaviour of df.to_markdown(showindex=False) would be nice.

@chrisjcameron are you interested in submitting a PR? If so, see the contributing guide for how to get started

@jreback
Copy link
Contributor

jreback commented Mar 13, 2020

adding index=False here would be ok (and translating to tabulate)

PR welcome

@quangngd
Copy link
Contributor

take

@quangngd
Copy link
Contributor

What should the behaviour be if user set both showindex and index? Which one should take the priority or should I raise an ValueError

@mproszewska
Copy link
Contributor

Does it make sense to add the index parameter if the showindex parameter already exists and will be used in the same way?
I can submit PR with added examples

@chrisjcameron
Copy link
Author

It would be more consistent with the other pandas to_* methods if the parameter to control including the index was called index rather than showindex.

@chrisjcameron
Copy link
Author

What should the behaviour be if user set both showindex and index? Which one should take the priority or should I raise an ValueError

I think most users will be expecting pandas-like keywords to work, so I would prioritize those. An alternative might be a warning about "When both index and showindex are set, one value will be used but which is undefined". This would at least give users a clue that these need not both be specified but gives a lot of latitude for future refinement that changes the behavior.

@jreback jreback added this to the 1.1 milestone Mar 29, 2020
@jreback jreback added the IO Data IO issues that don't fit into a more specific label label Mar 29, 2020
@franasa
Copy link

franasa commented May 26, 2020

thumbs up for this enhancement to the docs. l landed here looking exactly for that

pandas/pandas/core/frame.py

Lines 1996 to 2024 in 3adf334

@Appender(
"""
Examples
--------
>>> df = pd.DataFrame(
... data={"animal_1": ["elk", "pig"], "animal_2": ["dog", "quetzal"]}
... )
>>> print(df.to_markdown())
| | animal_1 | animal_2 |
|---:|:-----------|:-----------|
| 0 | elk | dog |
| 1 | pig | quetzal |
"""
)
@Substitution(klass="DataFrame")
@Appender(_shared_docs["to_markdown"])
def to_markdown(
self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs
) -> Optional[str]:
kwargs.setdefault("headers", "keys")
kwargs.setdefault("tablefmt", "pipe")
tabulate = import_optional_dependency("tabulate")
result = tabulate.tabulate(self, **kwargs)
if buf is None:
return result
buf, _, _, _ = get_filepath_or_buffer(buf, mode=mode)
assert buf is not None # Help mypy.
buf.writelines(result)
return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants