I want to update DataFrame regularly and accumulate information.

Asked 2 years ago, Updated 2 years ago, 43 views

Program Overview

Using the API of the smartphone game, data on the results of the match are collected and put into dataframe.
ここThe part that runs the program automatically on a regular basis is not planned here.

Specifically
①Create an empty data frame
②Get the information you need
③Use を to summarize into Series
④を Add to empty frame
You are about to do .

What I don't know

How to write code that runs functions periodically and accumulates data in DateFrame.
In particular, when you try again after a certain period of time after the first run, the data frame is empty, so the data cannot be accumulated.

コード There is no error in how to write the code.

Actual Code

def selfcheck_list(name):

    columns1=["type", "my", "opponent", "result", "time"]#1
    df=pd.DataFrame(columns=columns1)

    ba=battle_info(name)#From here, part 2
    datalist=[ ]
    for newnum in range (0,25):

        mydecklist=[ba[decknum]["team"][0]["cards"][numindeck]["name"]for decknum in range(0,25)for numindeck in range(0,8)]
        opodecklist=[ba[decknum]["opponent"][0]["cards"][numindeck]["name"]for decknum in range(0,25)for numindeck in range(0,8)]
        decktype=[ba[decknum]["type"] for decknum in range (0,25)]
        mycrowns=[ba[decknum]["team"][0]["crowns"]for decknum in range(0,25)]
        opocrowns=[ba[decknum]["opponent"][0]["crowns"]for decknum in range(0,25)]
        time = [ba[decknum]["battleTime"] for decknum in range(0,25)]

        a = decktype [int(newnum)]
        b=tuple(mydecklist[int(newnum*8):int(newnum*8+8)])
        c=tuple(opodecklist [int(newnum*8): int(newnum*8+8)])
        if mycrows [int(newnum)] > opocrows [int(newnum)]:
            winorlose="win"
        elif mycrows [int(newnum)] <opocrows [int(newnum)]:
            winorlose="lose"
        else:
            winorlose="draw"
        d = winorlose
        e=time [int(newnum)] [:15]
        data = [a, b, c, d, e ]
        datalist.append(data)
        record=pd.Series(datalist)#3 part
        df=df.append(record, ignore_index=True)#4


whole code


import json
import requests
import pandas aspd
import numpy as np

access_key=""

URL='https://api.clashroyale.com/v1'

# Create a dictionary that combines player names and paths
dic={"Tangerine Boy": "%232VYJYJ09", "Ten God": "%232G0QUGLU", "kota": "%23889VQ8JP", "RAD": "%238QRCJQ9Y", "Like Jones": "%2398Q8LPQ9",
    "Jack": "%23YRVL9U98", "Kitashin": "%23P8RLYOV9", "Dani": "%238LJVVGJP", "Kenzushi": "%23PQR0CG9",
    "Rorapolon": "%239JPRJ9R", "Yakitori": "%232Y8GL0V2", "Yuihiro": "%23R2GRQPCJ", "Blossom": "%238Q20LRC8Y", "kkk19212": "%23RU2CC2LG",
    "Reya": "%232LRVG0C8", "HANE×HANE": "%238Y088VU8U", "Lewis": "%238Q020U0U", "Pirameki": "%232YGGGY92V", "Tempura": "%238Q2V2CGR", "Scott": "%232Q98GVP9V"}

# Create a list containing player names
list=["Tangerine Boy", "Ten God", "kota", "RAD", "Like Jones",
    "Jack", "Kitashin", "Dani", "Kenzushi",
    "Rorapolon", "Yakitori", "Yuihiro", "Blossom", "kk19212",
    "Reiya", "HANE×HANE", "Lewis", "Pirameki", "Tempura", "Scott"]


def battle_info(name):
    target_api=URL+"/players/"
    playerTag=dic [name]
    url=target_api+playerTag+"/battlelog"
    headers = {
        "content-type": "application/json; charset=utf-8",
        "cache-control": "max-age=60",
        "authorization": "Bearer%s" %access_key}
    r=requests.get(url,headers=headers)
    data=r.json()
    return data

__name__=='_battle_info_'


python pandas

2022-09-30 20:13

1 Answers

How to write code that runs functions periodically and accumulates data in DateFrame.
In particular, when you try again after a certain period of time after the first run, the data frame is empty, so the data cannot be accumulated.

I will omit a lot of things, but I think the following code can be used.

import pandas as pd

class TestClass:

    def__init__(self):
        self.__df = pd.DataFrame([])

    def battle_info(self,name):

        # omission

        return data

    def selfdecck_list(self,name):
        ba=self.battle_info(name)

        # omission

        record=pd.Series (datalist)
        self.__df = self.__df.append(record, ignore_index=True)

    def polling (self):
        while True:
            # Run function every 5 seconds
            time.sleep (5.0)
            self.selfdeck_list("abc")

if__name__=='__main__':
    cls = TestClass()
    cls.polling()

If you want to invoke selfdecck_list at any time and with any arguments,
You can generate cls=TestClass() at the destination and call cls.selfdecck_list().


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.