如何实现表格底部添加D/T、S/T百分比行的需求?
How to Add D/T and S/T Percentage Rows to Your Table
Absolutely, this request is completely achievable! Whether you're calculating manually, using a spreadsheet tool, or writing a quick script, you can generate those percentage rows easily. Here's how to go about it:
Manual Calculation Approach
For each column, divide the value from the D row by the corresponding value in the T row, and repeat the same for the S row divided by T. Round the results to three decimal places (matching your desired output):
- Column a: D/T = 2168 / 13208 ≈ 0.164; S/T = 435 / 13208 ≈ 0.033
- Column b: D/T = 1545 / 5438 ≈ 0.284; S/T = 212 / 5438 ≈ 0.040
- Column c: D/T = 2163 / 12935 ≈ 0.167; S/T = 410 / 12935 ≈ 0.032
- Column d: D/T = 1540 / 5337 ≈ 0.288; S/T = 193 / 5337 ≈ 0.036 (note: your desired output shows 0.04, which is a rounding to two decimal places—adjust based on your preference)
- Column e: D/T = 2153 / 12656 ≈ 0.170; S/T = 391 / 12656 ≈ 0.031
Spreadsheet Tool (Excel/Google Sheets)
If you're using a spreadsheet tool, this is straightforward:
- In cell A5, type
D/T; in cell A6, typeS/T - In cell B5, enter the formula
=B2/B4(assuming D is in row 2, T is in row 4) - Drag the fill handle from B5 to F5 to apply the formula across all columns
- Repeat for cell B6 with
=B3/B4, then drag to F6 - Format the cells to show 3 decimal places (adjust to 2 if you want values like 0.04 instead of 0.036)
Python Script (Using Pandas)
If you prefer using code to automate this, here's a quick pandas snippet:
import pandas as pd # Create the original table data data = { 'SD': ['D', 'S', 'T'], 'a': [2168, 435, 13208], 'b': [1545, 212, 5438], 'c': [2163, 410, 12935], 'd': [1540, 193, 5337], 'e': [2153, 391, 12656] } df = pd.DataFrame(data).set_index('SD') # Calculate the percentage rows df.loc['D/T'] = df.loc['D'] / df.loc['T'] df.loc['S/T'] = df.loc['S'] / df.loc['T'] # Round to 3 decimal places (change to 2 if needed) df = df.round(3) # Print the final table print(df.reset_index().to_string(index=False))
Final Formatted Table (Matching Your Desired Output)
| SD | a | b | c | d | e |
|---|---|---|---|---|---|
| D | 2168 | 1545 | 2163 | 1540 | 2153 |
| S | 435 | 212 | 410 | 193 | 391 |
| T | 13208 | 5438 | 12935 | 5337 | 12656 |
| D/T | 0.164 | 0.284 | 0.167 | 0.288 | 0.170 |
| S/T | 0.033 | 0.040 | 0.032 | 0.036 | 0.031 |
内容的提问来源于stack exchange,提问作者Becky Purland
相关产品推荐
相关产品推荐

