Python数据分析 知识量:13 - 56 - 232
在数据表对象中插入新的列可以使用insert()函数来实现,其参数为:插入位置、新列的名称和插入的数据。下面是一个示例:
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') df.insert(2,'Age',[15,14,15,12,11,16,15,14]) print(df)
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 Name Sex Age Chinese English Math 0 Noah male 15 90 50 66 1 Emma female 14 56 56 55 2 Bob male 15 99 84 89 3 Olivia female 12 86 87 44 4 Jeff male 11 48 87 65 5 Liam male 16 55 88 69 6 Sophia female 15 90 66 96 7 Isabella female 14 66 85 55
以上示例中,在第2列(从0开始计数)插入新列Age,并添加了对应的值。注意:新值的数量与原表中的行数必须一致。
Python中没有专门用于在数据表对象中插入行的函数,但可以将新行视为一个新表,然后把原表与新表在纵轴方向上拼接为最终的表。详细方法参考表的拼接部分。
DataFrame对象的行列互换类似于Excel中的数据转置,即将行列的位置对调,数据也随之切换位置。可以使用.T方法来实现行列的互换。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.T)
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 0 1 2 3 4 5 6 7 Name Noah Emma Bob Olivia Jeff Liam Sophia Isabella Sex male female male female male male female female Chinese 90 56 99 86 48 55 90 66 English 50 56 84 87 87 88 66 85 Math 66 55 89 44 65 69 96 55
索引重构就是将原索引进行重新构造。在DataFrame对象中,通过行索引+列索引来确定一个唯一值,如果将列索引也看做是一个行索引,从而形成一对索引的形式就可以把DataFrame对象结构转换为一个树形结构,这个过程称为索引重构或索引重塑。
在Python中,通过stack()方法来实现索引的重构。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.stack())
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 0 Name Noah Sex male Chinese 90 English 50 Math 66 1 Name Emma Sex female Chinese 56 English 56 Math 55 2 Name Bob Sex male Chinese 99 English 84 Math 89 3 Name Olivia Sex female Chinese 86 English 87 Math 44 4 Name Jeff Sex male Chinese 48 English 87 Math 65 5 Name Liam Sex male Chinese 55 English 88 Math 69 6 Name Sophia Sex female Chinese 90 English 66 Math 96 7 Name Isabella Sex female Chinese 66 English 85 Math 55 dtype: object
stack()函数的逆函数为unstack(),可以将树形数据转换为表格型数据。
长宽表转换是指将比较长(很多行)的表或比较宽(很多列)的表,转换为比较宽或比较长的表,即在横向和纵向上对表格进行调整。
使用melt()函数可以实现宽表转长表。其参数的说明如下:
id_vars用于指定保持不变的列;
var_name用于指定转换的列的新名称;
value_name用于指定新索引对应的值的列名。
下面是一个示例:
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.melt(id_vars=['Name','Sex'],var_name='Test',value_name='Score'))
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 Name Sex Test Score 0 Noah male Chinese 90 1 Emma female Chinese 56 2 Bob male Chinese 99 3 Olivia female Chinese 86 4 Jeff male Chinese 48 5 Liam male Chinese 55 6 Sophia female Chinese 90 7 Isabella female Chinese 66 8 Noah male English 50 9 Emma female English 56 10 Bob male English 84 11 Olivia female English 87 12 Jeff male English 87 13 Liam male English 88 14 Sophia female English 66 15 Isabella female English 85 16 Noah male Math 66 17 Emma female Math 55 18 Bob male Math 89 19 Olivia female Math 44 20 Jeff male Math 65 21 Liam male Math 69 22 Sophia female Math 96 23 Isabella female Math 55
长表转换为宽表是宽表转换为长表的逆过程。可以利用数据透视表的功能来实现,使用pivot_table()函数,具体解释参考数据透视表部分。下面是一个简单的示例:
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") new_df=df.melt(id_vars=['Name','Sex'],var_name='Test',value_name='Score') print(new_df,'\n') print(new_df.pivot_table(index=['Name','Sex'],columns='Test',values='Score'))
运行结果为:
Name Sex Test Score 0 Noah male Chinese 90 1 Emma female Chinese 56 2 Bob male Chinese 99 3 Olivia female Chinese 86 4 Jeff male Chinese 48 5 Liam male Chinese 55 6 Sophia female Chinese 90 7 Isabella female Chinese 66 8 Noah male English 50 9 Emma female English 56 10 Bob male English 84 11 Olivia female English 87 12 Jeff male English 87 13 Liam male English 88 14 Sophia female English 66 15 Isabella female English 85 16 Noah male Math 66 17 Emma female Math 55 18 Bob male Math 89 19 Olivia female Math 44 20 Jeff male Math 65 21 Liam male Math 69 22 Sophia female Math 96 23 Isabella female Math 55 Test Chinese English Math Name Sex Bob male 99 84 89 Emma female 56 56 55 Isabella female 66 85 55 Jeff male 48 87 65 Liam male 55 88 69 Noah male 90 50 66 Olivia female 86 87 44 Sophia female 90 66 96
Copyright © 2017-Now pnotes.cn. All Rights Reserved.
编程学习笔记 保留所有权利
MARK:3.0.0.20240214.P35
From 2017.2.6