其他分享
首页 > 其他分享> > 体育收入排行2012-2019(用pandas)

体育收入排行2012-2019(用pandas)

作者:互联网

import pandas as pd
import numpy as np

def fun1(n):
    k = int(input())
    data = pd.read_csv('2012-19sport.csv', index_col=0)
    data_tentative = data.loc[data['Year'] == n]
    # data_tentative = data_tentative.set_index('Name')
    data_tentative = data_tentative.copy()  # 报错解决方案A value is trying to be set on a copy of a slice from a DataFrame.
    # Try using .loc[row_indexer,col_indexer] = value instead
    data_tentative['Rank'] = range(1, len(data_tentative) + 1)
    data_tentative = data_tentative.set_index('Rank')
    data_tentative['Rank'] = range(1, len(data_tentative) + 1)
    df_Rank = data_tentative['Rank']
    data_tentative = data_tentative.drop('Rank', axis=1)
    data_tentative.insert(0, 'Rank', df_Rank)
    ls1_np = np.array(data_tentative.iloc[:k])
    ls1 = ls1_np.tolist()
    for element in ls1:
        print(*element, sep=' | ')


def fun2():
    year = int(input())
    data = pd.read_csv('2012-19sport.csv', index_col=0)
    data_one_year = data.loc[data['Year'] == year]
    ls_Sport = sorted(data_one_year['Sport'].unique())
    dt_Sport = dict(zip([i for i in range(1, len(ls_Sport) + 1)], ls_Sport))
    print(dt_Sport)
    for element in ls_Sport:
        print(f'{ls_Sport.index(element) + 1}: {element}')
    key = int(input())
    # print(data_one_year)
    # print(dt_Sport[key])
    data_Sport = data_one_year.loc[data_one_year['Sport'] == dt_Sport[key]]
    # print(data_Sport.head)
    # print(data_Sport.index)  # 这句代码可以获得行索引列表
    # print(data_Sport.index)  # 这句代码可以获得列索引列表
    ls_Rank = list(data_Sport.index)
    # print(type(data_Sport['Pay']))
    # print(list(data_Sport['Pay']))

    print(list(data_Sport))
    ls_np = np.array(data_Sport)
    ls = ls_np.tolist()
    print(ls)
    # for i in range(len(ls_Rank)):
    #     ls[i].insert(0, ls_Rank[i][1:])
    # for element in ls:
    #     print(*element, sep=' | ')
    #
    # ls_np = np.array(data_Sport['Pay'])
    # ls = ls_np.tolist()
    # for i in range(len(ls)):
    #     ls[i] = float(ls[i].replace('$', '').replace(' M', ''))
    # # print(ls)
    # print('TOTAL: ${:.2f} M'.format(sum(ls)))


fun2()
if __name__ == '__main__':
    option = input()
    try:
        option = int(option)
        if 2012 <= option <= 2019:
            fun1(option)
        else:
            print('Wrong Input')
    except Exception as e:
        option = option.lower()
        if option == 'sport':
            fun2()
        else:
            print('Wrong Input')

标签:tentative,Rank,2019,ls,print,Sport,data,pandas,2012
来源: https://blog.csdn.net/Zzzzzz_m/article/details/122766312