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

added markDown Feature #60004

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions markDown/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# DataFrame and Series to Markdown Converter

## Overview

This package provides utility functions to convert pandas DataFrames and Series into markdown-formatted tables. These functions are integrated into the pandas library as methods for seamless usage.

## Installation

Ensure you have pandas installed:
```bash
pip install pandas
```

1. Clone the Repository

2. Import the Module: In you Python script or Jupyter Notebook, you can import the markDown.py module like so:

```python
from markDown import markdownTable, markdownSeries
```
3. Use the Functions: Once imported, you can use the `markdownTable` and `markdownSeries` functions directly, or you can utilize the extended functionality added to Pandas DataFrame and Series.

For example, to convert a DataFrame to a markdown table:

```python
import pandas as pd

# Assuming 'df' is the DataFrame you want to convert
markdown_table = df.markdownTable()
```
Or, to convert a Series to a markdown table:

```python
import pandas as pd

# Assuming 'series' is the Series you want to convert
markdown_table = series.markdownSeries()
```

Users can also specify specific columns for DataFrame conversion:

```python
markdown_table = df.markdownTable('Column1', 'Column2', 'Column3')
```

Or customize the column names for Series conversion:

```python
markdown_table = series.markdownTable(col1='Index', col2='Value')
```
## Conclusion
These functions provide an easy way to convert pandas DataFrames and Series into markdown-formatted tables, enhancing the readability and presentation of your data in markdown-supported environments. This is simular to using `to_markdown` in pandas without having to install another library.

pandas, the `to_markdown` method is provided by the `tabulate` library, which needs to be installed separately. The to_markdown method is available in pandas version 1.3.0 and later.

27 changes: 27 additions & 0 deletions markDown/markDown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pandas as pd


def markdownTable(df, *column_names):
if not column_names:
column_names = df.columns.tolist()
table_markdown = "| " + " | ".join(column_names) + " |\n"
table_markdown += "| " + " | ".join(["---"] * len(column_names)) + " |\n"
for index, row in df.iterrows():
row_data = [str(row[col]) for col in column_names]
table_markdown += "| " + " | ".join(row_data) + " |\n"
return table_markdown


def markdownSeries(series, col1=None, col2=None):
if not col1:
col1 = series.index.name if series.index.name else "Index"
if not col2:
col2 = series.name if series.name else "Value"
table_markdown = f"| {col1} | {col2} |\n|---|---|\n"
for index, value in series.items():
table_markdown += f"| {index} | {value} |\n"
return table_markdown


pd.DataFrame.markdownTable = markdownTable
pd.Series.markdownTable = markdownSeries
25 changes: 25 additions & 0 deletions markDown/sample_use.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pandas as pd
from markDown import markdownTable, markdownSeries

# Generic Grade book data
data = {
'Student': [f'Student {i}' for i in range(1, 21)],
'Math': [round(50 + i + (i % 3) * 5, 1) for i in range(1, 21)],
'Science': [round(55 + i + (i % 4) * 3.5, 1) for i in range(1, 21)],
'English': [round(60 + i + (i % 2) * 2.7, 1) for i in range(1, 21)],
'History': [round(65 + i + (i % 5) * 4.2, 1) for i in range(1, 21)]
}
df = pd.DataFrame(data)

# Convert the DataFrame to a markdown table
markdown_table = markdownTable(df)
print(f"Convert the DataFrame to a markdown table\n{markdown_table}")

# Convert the 'Math' Series to a markdown table
math_series_markdown = markdownSeries(df['Math'], col1='Student', col2='Math Grade') # noqa
print(f"Convert the 'Math' Series to a markdown table\n{math_series_markdown}")

# Print value counts of 'Student' column in a markdown table format
value_counts_series = df['Student'].value_counts().sort_values(ascending=False)
markdown_table = markdownSeries(value_counts_series, col1='Student', col2='Count') # noqa
print(f"Print value counts of 'Student' column in a markdown table format\n{markdown_table}") # noqa
32 changes: 32 additions & 0 deletions markDown/sample_use2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pandas as pd
from markDown import markdownTable, markdownSeries

# Create a hypothetical dataset
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [25, 30, 35, 40, 45],
'Gender': ['Female', 'Male', 'Male', 'Male', 'Female'],
'Salary': [50000, 60000, 70000, 80000, 90000],
'Department': ['HR', 'Finance', 'IT', 'Marketing', 'Operations']
}
df = pd.DataFrame(data)

# Create a new column 'Bonus' based on complex calculation
df['Bonus'] = df['Salary'] * 0.1 + df['Age'] * 0.05

# Perform some complex operations on the DataFrame
# For example, let's filter the DataFrame for individuals with Age > 30 and Salary > 60000
filtered_df = df[(df['Age'] > 30) & (df['Salary'] > 60000)]

# Convert the filtered DataFrame to a markdown table
filtered_markdown_table = markdownTable(filtered_df)
print("Filtered DataFrame as Markdown Table:")
print(filtered_markdown_table)

# Calculate the average Bonus for each gender
avg_bonus_by_gender = df.groupby('Gender')['Bonus'].mean()

# Convert the Series to a markdown table
avg_bonus_markdown = markdownSeries(avg_bonus_by_gender, col1='Gender', col2='Average Bonus')
print("\nAverage Bonus by Gender as Markdown Table:")
print(avg_bonus_markdown)
Loading
Loading