pandas中的 Series的讲解

# coding=utf-8


import pandas as pd
import numpy as np
from pandas import Series
''' 
    Series的方法与属性
        属性:
            values 获取某一列的数据值 获取的值为numpy.ndarray类型
            index 获取series数据
            
        方法:
            Series(数值项,index=索引的项)  #数值项 与索引的项必须一一对应 ,索引项可以为字符串
            index.tolist() names
            sort_index()  用索引项排序
            sort_values() 用数值项排序
'''


#Series (collection of values)
#DataFrame (collection of Series objects)
#Panel (collection of DataFrame objects)

#A Series object can hold many data types, including
#float - for representing float values
#int - for representing integer values
#bool - for representing Boolean values
#datetime64[ns] - for representing date & time, without time-zone
#datetime64[ns, tz] - for representing date & time, with time-zone
#timedelta[ns] - for representing differences in dates & times (seconds, minutes, etc.)
#category - for representing categorical values
#object - for representing String values

#FILM - film name
#RottenTomatoes - Rotten Tomatoes critics average score
#RottenTomatoes_User - Rotten Tomatoes user average score
#RT_norm - Rotten Tomatoes critics average score (normalized to a 0 to 5 point system)
#RT_user_norm - Rotten Tomatoes user average score (normalized to a 0 to 5 point system)
#Metacritic - Metacritic critics average score
#Metacritic_User - Metacritic user average score


#Series相当与DataFrame的一行或一列

fandango  = pd.read_csv('fandango_score_comparison.csv')
series_film = fandango['FILM'] #取出 FILM这一列
#print(type(series_film)) #class 'pandas.core.series.Series'
#print(series_film[0:5])
series_rt = fandango['RottenTomatoes']
#print(series_rt[0:5])

film_names = series_film.values #获取
#print(type(film_names))
rt_scores = series_rt.values
series_custom = Series(rt_scores , index=film_names)
series_custom[['Minions (2015)', 'Leviathan (2014)']] #以film_names为参数进行索引
fiveten = series_custom[5:10]
#print(fiveten)

#reindex更多的不是修改pandas对象的索引,而只是修改索引的顺序,
# 如果修改的索引不存在就会使用默认的None代替此行。且不会修改原数组,
# 要修改需要使用赋值语句。
original_index = series_custom.index.tolist()
#print(original_index)
sorted_index = sorted(original_index)
#print(sorted_index)
sorted_by_index = series_custom.reindex(sorted_index)
#print(sorted_by_index)



sc2 = series_custom.sort_index()
#print(sc2[0:10])
sc3 = series_custom.sort_values()
#print(sc3[0:10])

#The values in a Series object are treated as an ndarray, the core data type in NumPy
import numpy as np
# Add each value with each other
print(np.add(series_custom, series_custom))   #add 对Series的value数值项,求和
# Apply sine function to each value
np.sin(series_custom) #对series_custom 的Series的value求sin函数
# Return the highest value (will return a single value not a Series)
print(np.max(series_custom))  #求最大值


# 计算value在50~75之间的数值
#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)


#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)


 
# coding=utf-8

import pandas as pd


#will return a new DataFrame that is indexed by the values in the specified column
#and will drop that column from the DataFrame
#without the FILM column dropped
fandango = pd.read_csv('fandango_score_comparison.csv')
print(type(fandango))
fandango_films = fandango.set_index('FILM', drop=False) #以set_index作为索引
print(fandango_films.index)


# Slice using either bracket notation or loc[]
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)']
fandango_films.loc[movies]
#When selecting multiple rows, a DataFrame is returned,
#but when selecting an individual row, a Series object is returned instead




#-------------------------
#The apply() method in Pandas allows us to specify Python logic
#The apply() method requires you to pass in a vectorized operation
#that can be applied over each Series object.
import numpy as np

# returns the data types as a Series
types = fandango_films.dtypes
#print types
# filter data types to just floats, index attributes returns just column names
float_columns = types[types.values == 'float64'].index
# use bracket notation to filter columns to just float columns
float_df = fandango_films[float_columns]
#print float_df
# `x` is a Series object representing a column
deviations = float_df.apply(lambda x: np.std(x))
print(deviations)



rt_mt_user = float_df[['RT_user_norm', 'Metacritic_user_nom']]
rt_mt_user.apply(lambda x: np.std(x), axis=1)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你开始讲解Pandas库时,你可以首先介绍Pandas是Python用于数据处理和分析的库。它提供了高效的数据结构,例如Series、DataFrame和Panel,并且可以进行数据清洗、合并、重塑和分组操作。它的目的是为了让数据分析更加简单、快速和直观。 接下来,可以向初学者展示如何安装Pandas。你可以在Python环境使用pip命令安装Pandas库。在终端输入以下命令: ``` pip install pandas ``` 安装成功后,你可以开始向初学者介绍Pandas的核心数据结构。Pandas最重要的数据结构是DataFrame,它类似于Excel表格,可以存储和处理二维数据。你可以向初学者展示如何创建DataFrame,例如: ``` import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie', 'David'], 'age': [20, 25, 30, 35], 'gender': ['F', 'M', 'M', 'M']} df = pd.DataFrame(data) print(df) ``` 这段代码将创建一个包含姓名、年龄和性别的DataFrame,并将其打印出来。你可以向初学者解释DataFrame的基本操作,例如选择、筛选和排序。例如,选择年龄列可以使用以下代码: ``` ages = df['age'] print(ages) ``` 你可以向初学者介绍如何使用Pandas进行数据清洗,例如删除重复行、处理缺失值和更改数据类型。例如,删除重复行可以使用以下代码: ``` df.drop_duplicates(inplace=True) ``` 最后,你可以向初学者展示如何使用Pandas进行数据分析。你可以使用Pandas提供的各种函数和方法来计算统计量、分组汇总和透视表等。例如,计算年龄的平均值可以使用以下代码: ``` mean_age = df['age'].mean() print(mean_age) ``` 通过这些简单的示例和操作,你可以帮助初学者了解Pandas库的基本概念和用法,并且可以启发他们进行更深入的数据处理和分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值