Python Forum
Make panda dataframe output pretty
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make panda dataframe output pretty
#1
Hi everyone

I got a bit further on my project that i got some help to in here: https://python-forum.io/Thread-Make-a-ta...son-output

Now i got my head bumped into the wall with the next set of task, so hope i can be guided in the right direction again in here :)

Not necessary to do the task in this order. All help is appreciated. I just have a small playbook with the stuff i would like to have, and ideas i come up with along the way. Then im sure im getting there at some point :)

What im trying to do is make it look a bit like the attached picture (not 100% like it, but some of it), it was just a api project that was interesting for me to begin with, and try and do my self. :) Since i will both work on api, and learn python. And learn a lot of stuff i can use for other projects.

Task1: I Want to make the tables easier to read. With all columns next to each other and results center aligned.
Task1.1: I want to put the table to some web output, so i got it in my localhost browser. (I have no idea how im going to do this, since i know nothing about it, but if its easier to just let it be as is, and then make it look pretty in browser i can live with that solution)

Task2: I got the extra columns added (SUresults) where i get return True/False, and then i have changed that to 1/0 instead. But what if the game ends draw. As it does in the first one 21-21. Then i want it to return 0.5 instead of the True(1)/False(0). How is that done ?

Task3: Add a new table to the program that looks at the SUresults column and give me output (in this case) 2-2-1 (for 2wins, 2loss, 1push) and also an output with the hit%. So it returns 40% wins in this case.

Got a few more task, but thats problably for a new thread later on. Think there is enough here to confuse, and enough i need to learn :)

The code:
import requests
import json
import pandas as pd

url = "http://api.sportsdatabase.com/nfl/query.json"

sdql = "date,day,week,season,team,o:team,site,points,o:points,line,total,OT@"
query = "season = 2018 and team = Browns and week < 6"
output = ''"json"''
api_key = ''"guest"''
string = sdql+query

querystring = {"sdql":string,"output":output,"api_key":api_key}

payload = ""

headers = {'User-Agent': 'curl/7.58.0',
           'Accept': '*/*'}

response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

temp = response.text[14:-3].replace("\t", "")
temp = temp.replace("\'", "\"""")

data = json.loads(temp)

d = data

record = {}
for i in zip(d['headers'], d['groups'][0]['columns']):
    record[i[0]]  = i[1]

d = record

pd.set_option('display.max_columns', None)

df = pd.DataFrame.from_dict(d)

df['SUresult'] = df['points'] > df['o:points'] # Add Straight Up results Column and see if team Won SU
df['SUresult'] = df['SUresult'].astype(int) # Change the Dataframe boolan to interger 0 or 1
print(df)
The Output:
 date       day  week  season    team    o:team  site  points  o:points  \
0  20180909    Sunday     1    2018  Browns  Steelers  home      21        21   
1  20180916    Sunday     2    2018  Browns    Saints  away      18        21   
2  20180920  Thursday     3    2018  Browns      Jets  home      21        17   
3  20180930    Sunday     4    2018  Browns   Raiders  away      42        45   
4  20181007    Sunday     5    2018  Browns    Ravens  home      12         9   

   line  total  OT  SUresult  
0   4.0   41.0   1         0  
1   9.0   49.0   0         0  
2  -3.0   39.5   0         1  
3   2.5   45.0   1         0  
4   2.5   45.5   1         1  

Process finished with exit code 0
Thanks all

Attached Files

Thumbnail(s)
   
Reply
#2
(Jan-16-2019, 09:44 AM)carstenlp Wrote: Task1: I Want to make the tables easier to read. With all columns next to each other and results center aligned.
Task1.1: I want to put the table to some web output, so i got it in my localhost browser. (I have no idea how im going to do this, since i know nothing about it, but if its easier to just let it be as is, and then make it look pretty in browser i can live with that solution)
Jupyter Notebook then your code output look like this,no print().
[Image: d4RUGP.jpg]
Here is your code shared from me with nbviewer

So Jupyter Notebook can make a lot stuff easier in the work with eg pandas,matplotlib...ect.
Can also share this easy as i have showed over.
Could run this myself with eg Flask on localhost or host,but that take a lot more work.
Reply
#3
Thanks snippsat

From what i understand, then jupyter runs the code and present it with help from nbviewer ? The output looks great.
Im problably just a "i want to run it in house on my own equipment" type of guy more than running it via jupyter and nbviewer. (also i got some ideas for other programs i will be used at work, there it has to run in house on localhost server) So thats why i also want to learn flask, and start getting my feed wet there with this task.

/ Carsten

(Jan-17-2019, 09:04 AM)snippsat Wrote:
(Jan-16-2019, 09:44 AM)carstenlp Wrote: Task1: I Want to make the tables easier to read. With all columns next to each other and results center aligned.
Task1.1: I want to put the table to some web output, so i got it in my localhost browser. (I have no idea how im going to do this, since i know nothing about it, but if its easier to just let it be as is, and then make it look pretty in browser i can live with that solution)
Jupyter Notebook then your code output look like this,no print().
[Image: d4RUGP.jpg]
Here is your code shared from me with nbviewer

So Jupyter Notebook can make a lot stuff easier in the work with eg pandas,matplotlib...ect.
Can also share this easy as i have showed over.
Could run this myself with eg Flask on localhost or host,but that take a lot more work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding a new column to a Panda Data Frame rsherry8 2 2,082 Jun-06-2021, 06:49 PM
Last Post: jefsummers
  No Output In Pandas DataFrame Query eddywinch82 1 1,903 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  How does pyplot know what was plotted by the output of pandas.DataFrame(...).cumprod( codeowl 2 2,163 Mar-28-2020, 08:27 AM
Last Post: j.crater
  How to filter data using a panda.DateFrame.loc pawlo392 1 2,627 May-27-2019, 08:47 PM
Last Post: michalmonday
  do you know a code that will print all correlation values using numpty and panda? crispybluewaffle88 1 2,418 Mar-06-2019, 12:45 PM
Last Post: scidam
  Panda Dataframe Rounding Issue ab0217 5 7,190 Nov-06-2018, 10:15 PM
Last Post: ichabod801
  Replacing values for specific columns in Panda data structure Padowan 1 14,634 Nov-27-2017, 08:21 PM
Last Post: Padowan
  Panda.read_cvs Issues Reading Certain Columns BlackHeart 5 6,089 Oct-27-2017, 04:29 PM
Last Post: Larz60+
  Panda Data Frame to Existing Multiple Sheets naveedraza 1 5,633 Jul-11-2017, 12:21 PM
Last Post: naveedraza

Forum Jump:

User Panel Messages

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