pyhton数据分析-pandas相关操作复习3-4

本文深入探讨了使用Python的Pandas库进行数据处理的高级技巧,包括如何通过merge()函数精确合并数据框,以及如何利用apply()函数进行数据转换和操作。通过实例演示了不同类型的合并(如左连接、右连接、全外连接)以及如何在数据框上应用自定义函数。

目录

3.Merging dataframes-merge()

4.Apply function

3.Merging dataframes-merge()

# create dummy data
df_a = pd.DataFrame({
        'subject_id': ['1', '2', '3', '4', '5'],
        'first_name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 
        'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches']})

df_b = pd.DataFrame({
        'subject_id': ['4', '5', '6', '7', '8'],
        'first_name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 
        'last_name': ['Bonder', 'Black', 'Balwner', 'Brice', 'Btisan']})

df_c = pd.DataFrame({
        'subject_id': ['1', '2', '3', '4', '5', '7', '8', '9', '10', '11'],
        'test_id': [51, 15, 15, 61, 16, 14, 15, 1, 61, 16]})
pd.merge(df_a,df_c,on = 'subject_id')
#交集
#out<<
	subject_id	first_name	last_name	test_id
0	1	Alex	Anderson	51
1	2	Amy	Ackerman	15
2	3	Allen	Ali	15
3	4	Alice	Aoni	61
4	5	Ayoung	Atiches	16
pd.merge(df_a,df_b,on = 'subject_id', how = 'outer')
#并集
subject_id	first_name_x	last_name_x	first_name_y	last_name_y
0	1	Alex	Anderson	NaN	NaN
1	2	Amy	Ackerman	NaN	NaN
2	3	Allen	Ali	NaN	NaN
3	4	Alice	Aoni	Billy	Bonder
4	5	Ayoung	Atiches	Brian	Black
5	6	NaN	NaN	Bran	Balwner
6	7	NaN	NaN	Bryce	Brice
7	8	NaN	NaN	Betty	Btisan
pd.merge(df_a, df_b, on='subject_id', how='right')
#以df_b数据为主 right
#out<<
subject_id	first_name_x	last_name_x	first_name_y	last_name_y
0	4	Alice	Aoni	Billy	Bonder
1	5	Ayoung	Atiches	Brian	Black
2	6	NaN	NaN	Bran	Balwner
3	7	NaN	NaN	Bryce	Brice
4	8	NaN	NaN	Betty	Btisan
pd.merge(df_a, df_b, on='subject_id', how='left')
#以df_a数据为主 left

4.Apply function

import pandas as pd
import numpy as np

#read the dataset
data_BM = pd.read_csv("bigmart_data.csv")
# drop the null values
data_BM = data_BM.dropna(how = "any")
# view the top results
#data_BM.head()
#accessing row wise
data_BM.apply(lamba x:x).head()
#按照行取出所有数据
#accessing first row 
data_BM.apply(lambda x:x[0])
#out<<
Item_Identifier                          FDA15
Item_Weight                                9.3
Item_Fat_Content                       Low Fat
Item_Visibility                      0.0160473
Item_Type                                Dairy
Item_MRP                               249.809
Outlet_Identifier                       OUT049
Outlet_Establishment_Year                 1999
Outlet_Size                             Medium
Outlet_Location_Type                    Tier 1
Outlet_Type                  Supermarket Type1
Item_Outlet_Sales                      3735.14
dtype: object
#accessing firdt column by index
#default axis = 0
data_BM.apply(lambda x:x[0],axis = 1).head()
#out<<
0       FDA15
1       DRC01
2       FDN15
4       NCD19
5       FDP36
#access by column name
#default axis = 0
data_BM.apply(lambda x:x['Item_Identifier'],axis = 1).head()
#out<<
0       FDA15
1       DRC01
2       FDN15
4       NCD19
5       FDP36

4.1小应用

常见于分类问题中,用于指定分类

def Outlet_Location_Type_fun(Outlet_Location_Type):
if Outlet_Location_Type == 'Tier 1':
	Outlet_Location_Type =1
elif Outlet_Location_Type == 'Tier 2':
	Outlet_Location_Type = 2
else:
	Outlet_Location_Type = 3
return Outlet_Location_Type
#access by column name
#default axis = 0
data_BM.apply(lamba x :Outlet_Location_Type_fun(x['Outlet_Location_Type'],axis =1).head())
0    1
1    3
2    1
4    3
5    3
dtype: int64

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值