Skip to content

Commit 163745c

Browse files
committed
added markDown Feature
1 parent 5126dca commit 163745c

File tree

5 files changed

+17130
-0
lines changed

5 files changed

+17130
-0
lines changed

markDown/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# DataFrame and Series to Markdown Converter
2+
3+
## Overview
4+
5+
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.
6+
7+
## Installation
8+
9+
Ensure you have pandas installed:
10+
```bash
11+
pip install pandas
12+
```
13+
14+
1. Clone the Repository
15+
16+
2. Import the Module: In you Python script or Jupyter Notebook, you can import the markDown.py module like so:
17+
18+
```python
19+
from markDown import markdownTable, markdownSeries
20+
```
21+
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.
22+
23+
For example, to convert a DataFrame to a markdown table:
24+
25+
```python
26+
import pandas as pd
27+
28+
# Assuming 'df' is the DataFrame you want to convert
29+
markdown_table = df.markdownTable()
30+
```
31+
Or, to convert a Series to a markdown table:
32+
33+
```python
34+
import pandas as pd
35+
36+
# Assuming 'series' is the Series you want to convert
37+
markdown_table = series.markdownSeries()
38+
```
39+
40+
Users can also specify specific columns for DataFrame conversion:
41+
42+
```python
43+
markdown_table = df.markdownTable('Column1', 'Column2', 'Column3')
44+
```
45+
46+
Or customize the column names for Series conversion:
47+
48+
```python
49+
markdown_table = series.markdownTable(col1='Index', col2='Value')
50+
```
51+
## Conclusion
52+
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.
53+
54+
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.
55+

markDown/markDown.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pandas as pd
2+
3+
4+
def markdownTable(df, *column_names):
5+
if not column_names:
6+
column_names = df.columns.tolist()
7+
table_markdown = "| " + " | ".join(column_names) + " |\n"
8+
table_markdown += "| " + " | ".join(["---"] * len(column_names)) + " |\n"
9+
for index, row in df.iterrows():
10+
row_data = [str(row[col]) for col in column_names]
11+
table_markdown += "| " + " | ".join(row_data) + " |\n"
12+
return table_markdown
13+
14+
15+
def markdownSeries(series, col1=None, col2=None):
16+
if not col1:
17+
col1 = series.index.name if series.index.name else "Index"
18+
if not col2:
19+
col2 = series.name if series.name else "Value"
20+
table_markdown = f"| {col1} | {col2} |\n|---|---|\n"
21+
for index, value in series.items():
22+
table_markdown += f"| {index} | {value} |\n"
23+
return table_markdown
24+
25+
26+
pd.DataFrame.markdownTable = markdownTable
27+
pd.Series.markdownTable = markdownSeries

markDown/sample_use.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pandas as pd
2+
from markDown import markdownTable, markdownSeries
3+
4+
# Generic Grade book data
5+
data = {
6+
'Student': [f'Student {i}' for i in range(1, 21)],
7+
'Math': [round(50 + i + (i % 3) * 5, 1) for i in range(1, 21)],
8+
'Science': [round(55 + i + (i % 4) * 3.5, 1) for i in range(1, 21)],
9+
'English': [round(60 + i + (i % 2) * 2.7, 1) for i in range(1, 21)],
10+
'History': [round(65 + i + (i % 5) * 4.2, 1) for i in range(1, 21)]
11+
}
12+
df = pd.DataFrame(data)
13+
14+
# Convert the DataFrame to a markdown table
15+
markdown_table = markdownTable(df)
16+
print(f"Convert the DataFrame to a markdown table\n{markdown_table}")
17+
18+
# Convert the 'Math' Series to a markdown table
19+
math_series_markdown = markdownSeries(df['Math'], col1='Student', col2='Math Grade') # noqa
20+
print(f"Convert the 'Math' Series to a markdown table\n{math_series_markdown}")
21+
22+
# Print value counts of 'Student' column in a markdown table format
23+
value_counts_series = df['Student'].value_counts().sort_values(ascending=False)
24+
markdown_table = markdownSeries(value_counts_series, col1='Student', col2='Count') # noqa
25+
print(f"Print value counts of 'Student' column in a markdown table format\n{markdown_table}") # noqa

markDown/sample_use2.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pandas as pd
2+
from markDown import markdownTable, markdownSeries
3+
4+
# Create a hypothetical dataset
5+
data = {
6+
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
7+
'Age': [25, 30, 35, 40, 45],
8+
'Gender': ['Female', 'Male', 'Male', 'Male', 'Female'],
9+
'Salary': [50000, 60000, 70000, 80000, 90000],
10+
'Department': ['HR', 'Finance', 'IT', 'Marketing', 'Operations']
11+
}
12+
df = pd.DataFrame(data)
13+
14+
# Create a new column 'Bonus' based on complex calculation
15+
df['Bonus'] = df['Salary'] * 0.1 + df['Age'] * 0.05
16+
17+
# Perform some complex operations on the DataFrame
18+
# For example, let's filter the DataFrame for individuals with Age > 30 and Salary > 60000
19+
filtered_df = df[(df['Age'] > 30) & (df['Salary'] > 60000)]
20+
21+
# Convert the filtered DataFrame to a markdown table
22+
filtered_markdown_table = markdownTable(filtered_df)
23+
print("Filtered DataFrame as Markdown Table:")
24+
print(filtered_markdown_table)
25+
26+
# Calculate the average Bonus for each gender
27+
avg_bonus_by_gender = df.groupby('Gender')['Bonus'].mean()
28+
29+
# Convert the Series to a markdown table
30+
avg_bonus_markdown = markdownSeries(avg_bonus_by_gender, col1='Gender', col2='Average Bonus')
31+
print("\nAverage Bonus by Gender as Markdown Table:")
32+
print(avg_bonus_markdown)

0 commit comments

Comments
 (0)