引包
import pandas as pd
pandas数据表表示方法
我想表示泰坦尼克号上的乘客的乘客。有很多乘客,我知道的有名字,年龄,和性别数据:
import pandas as pd
df = pd.DataFrame({
"Name":["Braund,Mr.Owen Harris",
"Allen,Mr.William Henry",
"Bonnell,Miss.Elizabeth"],
"Age":[22,35,58],
"Sex":["male","male","female"]})
print(df)
输出结果:
在这里插入代码片 Name Age Sex
0 Braund,Mr.Owen Harris 22 male
1 Allen,Mr.William Henry 35 male
2 Bonnell,Miss.Elizabeth 58 female
一个DataFrame是一个2维数据结构,这种结构可以存储不同类型(包括:字符、整型、浮点型、categorical数据等等),它像电子表格,一个SQL表。
在电子表格的软件中,非常像我们的数据。
在DataFrame中的每一列都是一个Series
获取df中的Age的列:
import pandas as pd
df = pd.DataFrame({
"Name":["Braund,Mr.Owen Harris",
"Allen,Mr.William Henry",
"Bonnell,Miss.Elizabeth"],
"Age":[22,35,58],
"Sex":["male","male","female"]})
print(df)
print("---------------------------")
print(df["Age"])
输出结果为:
Name Age Sex
0 Braund,Mr.Owen Harris 22 male
1 Allen,Mr.William Henry 35 male
2 Bonnell,Miss.Elizabeth 58 female
---------------------------
0 22
1 35
2 58
Name: Age, dtype: int64
当选择pandas中的DataFrame中的一列的时候,返回的结果是一个pandas的Series,要想选择这个列,可以在[]中使用这列的“列名+双引号”的方式。
注意:如果你熟悉Python中的dictionaries类型,选中的单列非常相似于它+key中。
import pandas as pd
ages = pd.Series([22,35,58],name="Age")
print(ages)
输出结果为:
0 22
1 35
2 58
Name: Age, dtype: int64
pandas的Series没有列的label,因为它只是一个DataFrame中的一列。一个Series也没有行的标签。
使用DataFrame和Series做一些事情
我想知道最大年龄的乘客
我们可以使用DataFrame,选择一列,并使用max()函数
import pandas as pd
df = pd.DataFrame({
"Name":["Braund,Mr.Owen Harris",
"Allen,Mr.William Henry",
"Bonnell,Miss.Elizabeth"],
"Age":[22,35,58],
"Sex":["male","male","female"]})
print(df)
print("---------------------------")
print(df["Age"].max())
输出结果为:
Name Age Sex
0 Braund,Mr.Owen Harris 22 male
1 Allen,Mr.William Henry 35 male
2 Bonnell,Miss.Elizabeth 58 female
---------------------------
58
使用describe获取DataFrame的总数量,标准差,平均值,最小值,最大值。
import pandas as pd
df = pd.DataFrame({
"Name":["Braund,Mr.Owen Harris",
"Allen,Mr.William Henry",
"Bonnell,Miss.Elizabeth"],
"Age":[22,35,58],
"Sex":["male","male","female"]})
print(df)
print("---------------------------")
print(df.describe())
输出结果:
Name Age Sex
0 Braund,Mr.Owen Harris 22 male
1 Allen,Mr.William Henry 35 male
2 Bonnell,Miss.Elizabeth 58 female
---------------------------
Age
count 3.000000
mean 38.333333
std 18.230012
min 22.000000
25% 28.500000
50% 35.000000
75% 46.500000
max 58.000000
再如:
import pandas as pd
titanic = pd.read_csv("foo.csv");
print(titanic)
print("-------------读取前8条数据----------------------")
print(titanic.head(8))
print("--------------读取后10条数据--------------------")
print(titanic.tail(10))
print("--------------显示每列的数据类型----------------")
print(titanic.dtypes)
print("--------------获取其中的几列数据----------------")
print(titanic[['A','B']])
print("--------------使用shape得到DF的维度信息--------")
print(titanic[["A","B"]].shape)
输出结果:
Unnamed: 0 A B C D
0 2013-01-01 -0.028544 -2.597953 -0.645116 0.403488
1 2013-01-02 -0.109636 -0.866292 -0.629185 1.072633
2 2013-01-03 -1.435202 -0.631815 1.208114 -1.647566
3 2013-01-04 0.368530 -1.073754 -0.712305 -0.513142
4 2013-01-05 0.813674 -0.081024 -1.153747 0.409363
.. ... ... ... ... ...
995 2015-09-23 0.783858 -0.330685 0.323741 1.767446
996 2015-09-24 0.017313 -1.792078 0.686136 0.122491
997 2015-09-25 0.100742 -1.802797 0.370563 1.297355
998 2015-09-26 -0.279896 -0.439861 -0.595908 -0.663100
999 2015-09-27 -0.519504 -1.476432 -0.877358 0.370039
[1000 rows x 5 columns]
-------------读取前8条数据----------------------
Unnamed: 0 A B C D
0 2013-01-01 -0.028544 -2.597953 -0.645116 0.403488
1 2013-01-02 -0.109636 -0.866292 -0.629185 1.072633
2 2013-01-03 -1.435202 -0.631815 1.208114 -1.647566
3 2013-01-04 0.368530 -1.073754 -0.712305 -0.513142
4 2013-01-05 0.813674 -0.081024 -1.153747 0.409363
5 2013-01-06 0.218026 0.284554 0.850285 -0.025403
6 2013-01-07 0.024721 0.068005 0.008694 -1.196519
7 2013-01-08 0.640028 1.153457 1.212075 1.371237
--------------读取后10条数据--------------------
Unnamed: 0 A B C D
990 2015-09-18 -1.009017 -0.478119 1.206474 0.788974
991 2015-09-19 -0.493941 0.243774 -0.766409 -0.661123
992 2015-09-20 -0.698089 -0.081359 -1.046874 0.125604
993 2015-09-21 0.249417 0.696110 0.961332 0.647499
994 2015-09-22 -0.410115 -0.554049 1.157751 1.499304
995 2015-09-23 0.783858 -0.330685 0.323741 1.767446
996 2015-09-24 0.017313 -1.792078 0.686136 0.122491
997 2015-09-25 0.100742 -1.802797 0.370563 1.297355
998 2015-09-26 -0.279896 -0.439861 -0.595908 -0.663100
999 2015-09-27 -0.519504 -1.476432 -0.877358 0.370039
--------------显示每列的数据类型----------------
Unnamed: 0 object
A float64
B float64
C float64
D float64
dtype: object
--------------获取其中的几列数据----------------
A B
0 -0.028544 -2.597953
1 -0.109636 -0.866292
2 -1.435202 -0.631815
3 0.368530 -1.073754
4 0.813674 -0.081024
.. ... ...
995 0.783858 -0.330685
996 0.017313 -1.792078
997 0.100742 -1.802797
998 -0.279896 -0.439861
999 -0.519504 -1.476432
[1000 rows x 2 columns]
--------------使用shape得到DF的维度信息--------
(1000, 2)