pannas详解

# coding=utf-8

'''
    pannas的函数作用;
        read_csv(文件的路径) 读取文件

    <class 'pandas.core.frame.DataFrame'>(read_csv返回的对象)对象的方法与属性:

        属性:
            columns输出所有的特征 与dtypes类似
            dtypes
            shape  [多少行,多少列]
            loc 获取每一个记录或切片获取,传入一个列表
                loc[1]  获取第二行
                loc[2:5] 获取3~6行
                loc[2,5,10] 获取第3,6,10行
                loc(行号,特征)  取出某一样元素
        方法:
            head(参数为读取的行数) 默认输出所有信息的前五行,也可以指定参数,显示几行
            max()  DataFrame['特征name'].max()
            min()  DataFrame['特征name'].min()
            mean() DataFrame['特征name'].mean()  平均值
            sort_values("Sodium_(mg)", inplace=True)  排序  ascending 默认为True 升序  False降序  NaN放在最后
            titanic_survival.pivot_table(index='Pclass',values='Survived',aggfunc=np.mean)  每一个不同类型的中所有Survived的平均值 默认aggfunc为平均值
            dropna() 判断是否有缺失值,有的话就舍去
            apply()自定义函数
'''

import pandas
    #read_csv 读取文件
food_info = pandas.read_csv('food_info.csv')
#print(food_info)
#print(type(food_info)) #<class 'pandas.core.frame.DataFrame'>
#print(food_info.dtypes) #输出每一个特征的类型,意思就是输出属性的类型
'''
    #object - For string values
    #int - For integer values
    #float - For float values
    #datetime - For time values
    #bool - For Boolean values
    #print(food_info.dtypes)
'''


    #head 默认输出所有信息的前五行,也可以指定参数,显示几行
first_rows = food_info.head()
#print(first_rows)
#print(food_info.head(3)) #显示前三行
#print(food_info.columns) #输出所有的特征 与dtypes类似
#print(food_info.shape) #行列



    #pandas uses zero-indexing
    #Series object representing the row at index 0.
    #loc 获取每一个记录或切片获取,传入一个列表
#print(food_info.loc[1])
    # Series object representing the seventh row.
    #food_info.loc[6]
    # Will throw an error: "KeyError: 'the label [8620] is not in the [index]'"
    #food_info.loc[8620]
    #The object dtype is equivalent to a string in Python
# Returns a DataFrame containing the rows at indexes 3, 4, 5, and 6.
#print(food_info.loc[3:6])
# Returns a DataFrame containing the rows at indexes 2, 5, and 10. Either of the following approaches will work.
# Method 1
#two_five_ten = [2,5,10]
#food_info.loc[two_five_ten]
# Method 2
#food_info.loc[[2,5,10]]



#获取指定的列
# Series object representing the "NDB_No" column.
ndb_col = food_info['NDB_No']
#print(ndb_col)
# Alternatively, you can access a column by passing in a string variable.
#col_name = "NDB_No"
#ndb_col = food_info[col_name]

#同时获取多个列
columns = ["Zinc_(mg)", "Copper_(mg)"]
zinc_copper = food_info[columns]
#print(zinc_copper)
# Skipping the assignment.
#zinc_copper = food_info[["Zinc_(mg)", "Copper_(mg)"]]


#输出含有单位(g)的特征的数据
col_names = food_info.columns.tolist() #tolist获取所有的特征name
print(col_names)
gram_colums = []
for c in col_names:
    if c.endswith('(g)'):
        gram_colums.append(c)
gram_df = food_info[gram_colums]
#print(gram_df)


#对数据某一列的操作
div_1000 = food_info['Iron_(mg)'] / 1000
print(div_1000)
# Adds 100 to each value in the column and returns a Series object.
#add_100 = food_info["Iron_(mg)"] + 100

# Subtracts 100 from each value in the column and returns a Series object.
#sub_100 = food_info["Iron_(mg)"] - 100

# Multiplies each value in the column by 2 and returns a Series object.
#mult_2 = food_info["Iron_(mg)"]*2

#两列进行相乘
#It applies the arithmetic operator to the first value in both columns, the second value in both columns, and so on
water_energy = food_info["Water_(g)"] * food_info["Energ_Kcal"]
water_energy = food_info["Water_(g)"] * food_info["Energ_Kcal"]
iron_grams = food_info["Iron_(mg)"] / 1000
food_info["Iron_(g)"] = iron_grams



# the "Vit_A_IU" column ranges from 0 to 100000, while the "Fiber_TD_(g)" column ranges from 0 to 79
#For certain calculations, columns like "Vit_A_IU" can have a greater effect on the result,
#due to the scale of the values
# The largest value in the "Energ_Kcal" column.
max_calories = food_info["Energ_Kcal"].max()
# Divide the values in "Energ_Kcal" by the largest value.
normalized_calories = food_info["Energ_Kcal"] / max_calories
normalized_protein = food_info["Protein_(g)"] / food_info["Protein_(g)"].max()
normalized_fat = food_info["Lipid_Tot_(g)"] / food_info["Lipid_Tot_(g)"].max()
#print(food_info.shape)
food_info["Normalized_Protein"] = normalized_protein
food_info["Normalized_Fat"] = normalized_fat
#print(food_info.shape)


#排序  ascending 默认为True 升序  False降序  NaN放在最后
#By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame
# Sorts the DataFrame in-place, rather than returning a new DataFrame.
#print food_info["Sodium_(mg)"]
food_info.sort_values("Sodium_(mg)", inplace=True)
print(food_info)
#Sorts by descending order, rather than ascending.
food_info.sort_values("Sodium_(mg)", inplace=True, ascending=False)
print(food_info["Sodium_(mg)"])














# coding=utf-8

import pandas as pd
import numpy as np


titanic_survival = pd.read_csv('titanic_train.csv')
#print(titanic_survival.head())

'''
#统计年龄为空的的记录数
age = titanic_survival['Age']
#print(age.loc[0:10])
age_is_null = pd.isnull(age) #若为Nan,则返回True,否则返回False
#print(age_is_null)
age_null_true = age[age_is_null] #得到为Nan的值
#print(age_null_true)
age_null_count = len(age_null_true)
print(age_null_count)

#The result of this is that mean_age would be nan. This is because any calculations we do with a null value also result in a null value
#错误的计算,因为sum中有的年龄为Nan
mean_age = sum(titanic_survival['Age']) / len(titanic_survival['Age'])
print(mean_age)

#正确的计算
good_ages = titanic_survival["Age"][age_is_null == False]
#print good_ages
correct_mean_age = sum(good_ages) / len(good_ages)
print(correct_mean_age)

# missing data is so common that many pandas methods automatically filter for it
correct_mean_age = titanic_survival["Age"].mean()
print(correct_mean_age)

#一等仓,二等仓,三等仓的平均价格
#几等仓的参数为Pclass,价格为Fare
#mean fare for each class
passenger_classes = [1, 2, 3]
fares_by_class = {}
for this_class in passenger_classes:
    pclass_rows = titanic_survival[titanic_survival["Pclass"] == this_class]
    pclass_fares = pclass_rows["Fare"]
    fare_for_class = pclass_fares.mean()
    fares_by_class[this_class] = fare_for_class
print(fares_by_class)


#index tells the method which column to group by
#values is the column that we want to apply the calculation to
#aggfunc specifies the calculation we want to perform
#每一个不同类型的中所有Survived的平均值
#默认aggfunc为平均值
passenger_survival = titanic_survival.pivot_table(
    index='Pclass',values='Survived',aggfunc=np.mean
)
print(passenger_survival)

'''

#Embarked代表登船的地点
port_stats = titanic_survival.pivot_table(index="Embarked", values=["Fare","Survived"], aggfunc=np.sum)
print(port_stats)

#specifying axis=1 or axis='columns' will drop any columns that have null values
#dropna 判断是否有缺失值,有的话就舍去
print(len(titanic_survival))
drop_na_columns = titanic_survival.dropna(axis=1)
new_titanic_survival = titanic_survival.dropna(axis=0,subset=["Age", "Sex"]) #subset有一个为Nan就丢弃掉
print(len(new_titanic_survival))

#loc[行,列]
row_index_83_age = titanic_survival.loc[83,"Age"]
row_index_1000_pclass = titanic_survival.loc[766,"Pclass"]
print(row_index_83_age)
print(row_index_1000_pclass)

#重新编序号
new_titanic_survival = titanic_survival.sort_values("Age",ascending=False)
print(new_titanic_survival[0:10])
titanic_reindexed = new_titanic_survival.reset_index(drop=True)
print(titanic_reindexed.iloc[0:10])


#自定义函数apply
# This function returns the hundredth item from a series
def hundredth_row(column):
    # Extract the hundredth item
    hundredth_item = column.iloc[99]
    return hundredth_item

# Return the hundredth item from each column
hundredth_row = titanic_survival.apply(hundredth_row)
print(hundredth_row)


def not_null_count(column):
    column_null = pd.isnull(column)
    null = column[column_null]
    return len(null)

column_null_count = titanic_survival.apply(not_null_count)
print(column_null_count)


#By passing in the axis=1 argument, we can use the DataFrame.apply() method to iterate over rows instead of columns.
def which_class(row):
    pclass = row['Pclass']
    if pd.isnull(pclass):
        return "Unknown"
    elif pclass == 1:
        return "First Class"
    elif pclass == 2:
        return "Second Class"
    elif pclass == 3:
        return "Third Class"

classes = titanic_survival.apply(which_class, axis=1)
print(classes)


def is_minor(row):
    if row["Age"] < 18:
        return True
    else:
        return False

minors = titanic_survival.apply(is_minor, axis=1)
#print minors

def generate_age_label(row):
    age = row["Age"]
    if pd.isnull(age):
        return "unknown"
    elif age < 18:
        return "minor"
    else:
        return "adult"

age_labels = titanic_survival.apply(generate_age_label, axis=1)
print(age_labels)


titanic_survival['age_labels'] = age_labels
age_group_survival = titanic_survival.pivot_table(index="age_labels", values="Survived")
print(age_group_survival)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值