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

被折叠的 条评论
为什么被折叠?



