Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print 3 columns from pandas data set in a table

Tags:

python

pandas

Say I have this data:

project:  group:  sum:
A         John    12
A         Sam     10
B         Sun     4
B         Toy     5
B         Joy     7
C         Sam     11

The data is in data set frame_main. I wanted to sum up by project so I did:

result_main = pd.concat(frame_main).groupby(["project","group"]).sum()

It basically doing what I wanted, which is summing up the third column and group by the first:

project:  group:  sum:
A         John    12
          Sam     10
B         Sun     4
          Toy     5
          Joy     7
C         Sam     11

But now when I'm trying to print it using the following:

print(tabulate(result_main, headers="keys", tablefmt='psql'))

It prints like that:

+---------------------------+-----------------+                                                                                       
|                           |   sum:          |                                                                                       
|---------------------------+-----------------|                                                                                       
| ('A', 'John')             |             12  |                                                                                       
| ('A', 'Sam')              |             10  |                                                                                       
| ('B', 'Sun')              |             4   |
| ('B', 'Toy')              |             5   |                                                                                       
| ('B', 'Joy')              |             7   |                                                                                       
| ('C', 'Sam')              |             11  |

How can I print so it would look like the output above? I need 3 columns and grouped by the first.

like image 777
jenny Avatar asked Jun 08 '20 21:06

jenny


People also ask

How do I display specific columns in a data frame?

If you have a DataFrame and would like to access or select a specific few rows/columns from that DataFrame, you can use square brackets or other advanced methods such as loc and iloc .

How do I print a list of columns in pandas?

To get the column names in Pandas dataframe you can type <code>print(df. columns)</code> given that your dataframe is named “df”.


1 Answers

Much like @Craig we can mask those duplicate value in 'project:' column.

df_sum = df_sum.reset_index()
df_sum['project:'] = df_sum['project:'].mask(df_sum['project:'].duplicated(),'')

print(df_sum.set_index('project:').to_markdown(tablefmt='psql'))

Output:

+------------+----------+--------+
| project:   | group:   |   sum: |
|------------+----------+--------|
| A          | John     |     12 |
|            | Sam      |     10 |
| B          | Sun      |      4 |
|            | Toy      |      5 |
|            | Joy      |      7 |
| C          | Sam      |     11 |
+------------+----------+--------+
like image 178
Scott Boston Avatar answered Oct 04 '22 01:10

Scott Boston