Python 3 Code Questions...

Asked 2 years ago, Updated 2 years ago, 85 views

// Enter your code here
import datetime # API for 4 weeks Top20 statistics Date Processing for real time

now = datetime.datetime.now() # Current date
weeks4 = now + datetime.timedelta(weeks=-4) #current date - 4 weeks
startDt = '%04d-%02d-%02d' % (weeks4.year, weeks4.month, weeks4.day)
endDt = '%04d-%02d-%02d' %(now.year, now.month, now.day) # Only the year, month, and date should be printed from the current date

apiKey = #APIKey variable

top20_url = f"http://data4library.kr/api/loanItemSrch?authKey={apiKey}&startDt={startDt}&endDt={endDt}&pageSize=20" #APIcallAddress top20_url variable

import pandas as pd
from bs4 import Beautiful Soup # Required for parsing tags
From urllib.request import urlopen #Required to retrieve Url address

data = urlopen(top20_url).Read() #top20_url is read and stored in the data variable
soup = BeautifulSoup(data, "html.parser") #Type parser via BeautifulSoup
find the docs tag in the bookitem = soup.find("docs") #Tag and save it in the bookitem

df = pd.DataFrame (columns = ['rank', book', category (type), 'rental', 'ISBN']) #Create an empty data frame


i = 0 # iInitial value
For items in bookitem.findAll("doc"): # Find the doc tag in the docs tag stored in the bookitem and put it in the items
    df.loc[i] = [items.ranking.text, items.bookname.text, items.class_no.text, items.loan_count.text, items.isbn13.text]
    i=i+1
df

I coded it like in the picture above. The output has categories (types) from 000 to 999.

So 000~099 : Hangul / 100~199 : English / 800~899 : Society I want to print it out in this way.

I've tried a lot of things. I don't know.(Crying)

if문 for python python3

2022-09-22 19:09

1 Answers

pandas.Series has a method called apply. A method that returns a series of results of the function by applying a function given by a factor for each element of the series.

pd.Series([1,2,3,4,5]).If you run apply("even number", "odd number")[e%2]) pd.Series (['Odd', 'Even', 'Odd', 'Even', 'Odd', 'Odd']) ).

For a value or string given by a number, you can create a function that returns 'Hangul', 'English', 'Society', etc. according to the given conditions, and then apply the function to the column of the number (class_no) to create a new column. Below is an example of a similar sample and running it on an idle.

>>> df = pd.DataFrame( { 'cat': [ '%03d'%i for i in pd.np.random.randint(0, 999, size=20) ] } )
>>> df
    cat
0   624
1   015
2   845
3   420
4   209
5   434
6   895
7   380
8   464
9   030
10  512
11  385
12  727
13  702
14  282
15  344
16  135
17  175
18  367
19  700
>>> def cat2str(cat):
    cat = int(cat[:3], 10)
    if cat < 100:
        S = "Hangul"
    elif cat < 300:
        s = 'English'
    elif cat < 800:
        s = "Fire"
    else:
        s = "I Don't Know"
    return s

>>> df['category'] = df['cat'].apply(cat2str)
>>> df
    cat category
0624. French
October 15 in Korean
2845. I don't know
3,420 French
4209 English
5434 French
6895. I don't know
7380
8464 French
9030 Hangul
Ten, five, twelve French
11385 French
12727 French
13702 French
14282 English
15344 French
16135 English
17175 English
18367 French
It's 1,700 dollars


2022-09-22 19:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.