微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐