pandas学习之Series结构
生活随笔
收集整理的這篇文章主要介紹了
pandas学习之Series结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/usr/bin/env python # -*- coding:utf-8 -*- """ 系列(值的集合) DataFrame數據包(系列對象的集合) panel(數據文件對象的集合) 一個系列對象可以保存許多數據類型,包括 浮點數表示浮點數 表示整數值的 布爾布爾值表示布爾值 表示日期和時間,沒有時區的 用日期時區表示日期和時間 表示時間和時間(秒,分鐘等)的差異的時間δ[NS] 表示類別值的類別 表示字符串值的對象 電影膠片名 # rottentomatoes -爛番茄影評平均得分 # rottentomatoes_user爛番茄用戶平均得分 # rt_norm -爛番茄影評平均得分(歸一化到0到5點的系統) # rt_user_norm爛西紅柿用戶平均得分(歸一化到0-5點系統) 亞元主義-元主義批評家平均分 亞元用戶-元用戶平均得分 """ #series結構 學習 import pandas as pd import numpy as np fandango = pd.read_csv('fandango_score_comparison.csv') series_film = fandango['FILM'] print(type(series_film)) print(series_film[0:5]) #FILM這一列前0-5個 series_rt = fandango['RottenTomatoes'] #RottenTomatoes前0-5個 print(series_rt[0:5])from pandas import Seriesfilm_names = series_film.values print(type(film_names)) #print(film_names) rt_scores = series_rt.values #得分值來自:RottenTomatoes #print(rt_scores) series_custom = Series(rt_scores,index=film_names) #以電影名字為索引 print(series_custom[['Minions (2015)','Leviathan (2014)']]) #輸出這兩部電影的評分 fiveten = series_custom[5:10] #以電影名字為索引,打印前5-10部電影和評分 print(fiveten)sc2 = series_custom.sort_index() sc3 = series_custom.sort_values() #以這兩種都可以進行排序 sc2[2:10} print(np.add(series_custom,series_custom)) #相加操作#will actually return a Series object with a boolean value for each film series_custom > 50 series_greater_than_50 = series_custom[series_custom > 50]criteria_one = series_custom > 50 criteria_two = series_custom < 75 #評分在這一段的電影和分數 both_criteria = series_custom[criteria_one & criteria_two] print(both_criteria) print('--------') #data alignment same index rt_critics = Series(fandango['RottenTomatoes'].values, index=fandango['FILM']) rt_users = Series(fandango['RottenTomatoes_User'].values, index=fandango['FILM']) rt_mean = (rt_critics + rt_users)/2 #兩個媒體評分的平均值 print(rt_mean) print('分割線——————') print(type(fandango)) fandango_films = fandango.set_index("FILM",drop=False) print(fandango_films.index) #打印出索引值 列名 #在選擇多行時,返回數據文件,但是選擇一個單獨的行時,將返回一個系列對象。 fandango_films["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"] fandango_films.loc["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"] # Specific movie fandango_films.loc['Kumiko, The Treasure Hunter (2015)'] # Selecting list of movies movies = ['Kumiko, The Treasure Hunter (2015)', 'Do You Believe? (2015)', 'Ant-Man (2015)'] print(fandango_films.loc[movies])print('繼續分割——————') types = fandango_films.dtypes float_columns = types[types.values == 'float64'].index #類型轉換 float_df = fandango_films[float_columns] deviations = float_df.apply(lambda x: np.std(x)) rt_mt_user = float_df[['RT_user_norm','Metacritic_user_nom']] rt_mt_user.apply(lambda x: np.std(x), axis=1) # 對以上兩個列做了變換 print(rt_mt_user)
?
轉載于:https://www.cnblogs.com/lifengwu/p/9816683.html
總結
以上是生活随笔為你收集整理的pandas学习之Series结构的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery 1.7.2源码解析(二)构
- 下一篇: 三星堆博物馆需要身份证吗