Python Forum

Full Version: Make panda dataframe output pretty
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
(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.
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.