Python Forum
How to insert Dashed Lines in between Rows of a tabulate output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to insert Dashed Lines in between Rows of a tabulate output
#1
Hi Im trying to format my report in python as per SAS proc report output. the expected output is shown below
Any idea how can i insert 1.dashed lines between rows. and 2. double dash in last two total rows?

my code below
import pandas as pd
from tabulate import tabulate

# Sample data (you should replace this with your actual data)
data = pd.read_csv(r"C:\\Users\\M68\\Downloads\\RV1.csv",delimiter="|")

# Create a DataFrame from the sample data
input_df = pd.DataFrame(data)

# Renaming the columns and uppercase
input_df.rename(columns={'_type_':'type'},inplace=True)
input_df.columns=input_df.columns.str.upper()

# Calculate the sums for each variable by 'TYPE' and 'SHPDT'
output_df = input_df.groupby(['TYPE', 'SHPDT']).agg({
    'REVENUE': 'sum',
    'REVPRTY': 'sum',
    'REVECON': 'sum',
    'C': 'sum',
    'TP': 'sum',
    'TE': 'sum'
}).reset_index()


# Rename the columns for the output DataFrame
output_df.rename(columns={
    'SHPDT' : 'SHIPPED DATE',
    'REVENUE': 'REVENUE\nALL SERVICES',
    'REVPRTY': 'REVENUE\nPRIORITY',
    'REVECON': 'REVENUE\nECONOMY',
    'C': 'PROS\nALL\nSERVICES',
    'TP': 'PROS\nPRIORITY',
    'TE': 'PROS\nECONOMY'
}, inplace=True)

# Convert the 'SHPDT' column to a date format
output_df['SHIPPED DATE'] = pd.to_datetime(output_df["SHIPPED DATE"]).dt.strftime('%d%b%y').str.upper()

# Sort the DataFrame by 'TYPE' and 'SHPDT'
output_df.sort_values(by=['TYPE', 'SHIPPED DATE'], inplace=True)

# Display the output DataFrame
print(output_df)

# Generate the tabular report
table = tabulate(output_df, headers='keys', tablefmt='grid')

# Print the tabular report
print(table)

# Generate the summary statistics (you can customize this part based on your needs)
summary_stats = output_df.groupby('TYPE').agg({
    'REVENUE\nALL SERVICES': 'sum',
    'REVENUE\nPRIORITY': 'sum',
    'REVENUE\nECONOMY': 'sum',
    'PROS\nALL\nSERVICES': 'sum',
    'PROS\nPRIORITY': 'sum',
    'PROS\nECONOMY': 'sum'
}).reset_index()

summary_stats1 = output_df.groupby('TYPE').agg({
    'REVENUE\nALL SERVICES': 'sum',
    'REVENUE\nPRIORITY': 'sum',
    'REVENUE\nECONOMY': 'sum',
    'PROS\nALL\nSERVICES': 'sum',
    'PROS\nPRIORITY': 'sum',
    'PROS\nECONOMY': 'sum'
}).reset_index()

# Display the summary statistics
print(summary_stats)

summary_stats['SHIPPED DATE'] = ''
summary_stats1['SHIPPED DATE'] = ''

report = pd.concat([output_df,summary_stats],keys=['output_df','summary_stats'])

ids = report['TYPE']

report['flag'] = ((ids!=ids.shift(1)) | (ids!=ids.shift(-1))).astype(int)

report.loc[report.flag == 0, 'TYPE'] = " "

report1 = pd.concat([report.reset_index(drop=True),summary_stats1])

# Keeping TYPE in last row as blank
report1.loc[report1.flag.isnull(), 'TYPE'] = " "

report1 = report1.drop(columns=['flag'])

# Addning comma between numeric values
report1['REVENUE\nALL SERVICES'] = report1['REVENUE\nALL SERVICES'].map('{:,.2f}'.format)
report1['REVENUE\nPRIORITY'] = report1['REVENUE\nPRIORITY'].map('{:,.2f}'.format)
report1['REVENUE\nECONOMY'] = report1['REVENUE\nECONOMY'].map('{:,.2f}'.format)
   
deanhystad write Sep-27-2023, 04:19 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.csv   RV1.csv (Size: 401 bytes / Downloads: 65)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why the blank lines in output? Mark17 6 1,445 Jun-27-2022, 11:21 PM
Last Post: Mark17
  pandas, tabulate, and alignment menator01 3 7,310 Feb-05-2022, 07:04 AM
Last Post: menator01
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,123 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,151 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  display the result of Dataframe in tabulate format alex80 0 1,395 Sep-09-2020, 02:22 PM
Last Post: alex80
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,878 Aug-10-2020, 11:01 PM
Last Post: medatib531
  How to tabulate correctly repeated blocks? Xiesxes 4 2,960 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  how to insert # to multiple lines? hkfatasy 1 2,903 Dec-22-2019, 01:51 AM
Last Post: ichabod801
  Trying to Tabulate Information from an Aircraft Website Link(s) eddywinch82 35 13,670 Jun-25-2019, 09:40 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020