体育收入排行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')

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...