美文网首页我爱编程
Python Beginners(5) - Pandas_ind

Python Beginners(5) - Pandas_ind

作者: 西瓜三茶 | 来源:发表于2017-06-16 21:04 被阅读0次

    1. Select Rows

    # First five rows
    fandango[0:5]
    # From row at 140 and higher
    fandango[140:]
    # Just row at index 50
    fandango.iloc[50]
    # Just row at index 45 and 90
    fandango.iloc[[45,90]]
    # Select first and last row
    fandango.iloc[[0,-1]]
    

    2. Set index

    fandango = pd.read_csv('fandango_score_comparison.csv')
    fandango_films = fandango.set_index("FILM", inplace = False, drop = False)
    print (fandango_films.index)
    

    loc(index_names)

    mlist = ["The Lazarus Effect (2015)", "Gett: The Trial of Viviane Amsalem (2015)", "Mr. Holmes (2015)"]
    best_movies_ever = fandango_films.loc[mlist]
    print (best_movies_ever)
    

    3.Apply Functions

    Apply functions.

    import pandas as pd
    import numpy as np
    
    fandango_films = pd.read_csv("fandango_score_comparison.csv")
    types = fandango_films.dtypes
    print (types)
    
    float_columns = types[types.values == "float64"].index
    print (float_columns)
    
    float_df = fandango_films[float_columns]
    
    print (float_df)
    
    deviations = float_df.apply(lambda x: np.std(x))
    print (deviations)
    

    4.Apply Functions along Rows

    rt_mt_user = float_df[['RT_user_norm', 'Metacritic_user_nom']]
    rt_mt_deviations = rt_mt_user.apply(lambda x: np.std(x), axis=1)
    print(rt_mt_deviations[0:5])
    rt_mt_means = rt_mt_user.apply(lambda x: np.mean(x), axis = 1)
    print (rt_mt_means[0:5])
    

    相关文章

      网友评论

        本文标题:Python Beginners(5) - Pandas_ind

        本文链接:https://www.haomeiwen.com/subject/gmmeqxtx.html