gpt4 book ai didi

python - 查找最大行数

转载 作者:行者123 更新时间:2023-11-28 18:18:27 30 4
gpt4 key购买 nike

我有一个以 csv 格式提取数据的 send_request() 函数。

from pull_data import send_request
from openpyxl import load_workbook
from datetime import datetime

def max_nb_rows(season_slug, urls_ws):
date = datetime.today().strftime('%Y%m%d')
for i in range(100):
d = []
response = send_request(season_slug, url_ws, date).content
df = pd.read_csv(io.StringIO(response.decode('utf-8')))
max_nb_rows('2015-2016-regular', 'cumulative-player-stats')

我想找出 df 连续 100 天的最大行数。我怎么能那样做?

最佳答案

我认为您需要先过滤今天的数据,然后通过 value_counts 获取最大计数,它对输出进行了排序,因此需要通过 iat 选择第一个值:

def max_nb_rows(season_slug, urls_ws):
lens = []
date = datetime.today().strftime('%Y-%m-%d')
for i in range(100):
d = []
response = send_request(season_slug, url_ws, date).content
df = pd.read_csv(io.StringIO(response.decode('utf-8')))
lens.append(df.loc[df['Date'] > date, 'Date'].value_counts().iat[0])
return max(lens)
max_nb_rows('2015-2016-regular', 'cumulative-player-stats')

示例:

df = pd.DataFrame({'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'Date':pd.to_datetime(['2017-10-21','2017-10-21','2017-10-21','2017-10-25','2017-10-25','2017-10-28'])})

print (df)
A B C D Date E
0 a 4 7 1 2017-10-21 5
1 b 5 8 3 2017-10-21 3
2 c 4 9 5 2017-10-21 6
3 d 5 4 7 2017-10-25 9
4 e 5 2 1 2017-10-25 2
5 f 4 3 0 2017-10-28 4

date = datetime.today().strftime('%Y-%m-%d')
a = df.loc[df['Date'] > date, 'Date'].value_counts().iat[0]
print (a)
2

详细信息:

print (df.loc[df['Date'] > date, 'Date'])
3 2017-10-25
4 2017-10-25
5 2017-10-28
Name: Date, dtype: datetime64[ns]

print (df.loc[df['Date'] > date, 'Date'].value_counts())
2017-10-25 2
2017-10-28 1
Name: Date, dtype: int64

关于python - 查找最大行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888883/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com