本文概述
Pandas DataFrame是一种广泛使用的数据结构, 可用于带有标记轴(行和列)的二维数组。 DataFrame被定义为存储具有两个不同索引(即行索引和列索引)的数据的标准方法。它包含以下属性:
- 这些列可以是异构类型, 例如int, bool等。
- 它可以看作是Series结构的字典, 其中行和列都被索引了。如果是列, 则表示为”列”;如果是行, 则表示为”索引”。
参数及说明
数据:它由ndarray, 系列, 映射, 常量, 列表, 数组等不同形式组成。
index:如果未传递索引, 则默认np.arrange(n)索引用于行标签。
columns:列标签的默认语法为np.arrange(n)。如果未传递索引, 则仅显示true。
dtype:指每一列的数据类型。
copy():用于复制数据。
创建一个DataFrame
我们可以使用以下方式创建一个DataFrame:
- 字典
- 清单
- 脾气暴躁的ndarrrays
- 系列
创建一个空的DataFrame
下面的代码显示了如何在Pandas中创建一个空的DataFrame:
# importing the pandas library
import pandas as pd
df = pd.DataFrame()
print (df)
输出
Empty DataFrame
Columns: []
Index: []
说明:在上面的代码中, 首先, 我们导入了别名为pd的pandas库, 然后定义了一个名为df的变量, 该变量包含一个空的DataFrame。最后, 我们通过将df传递到打印文件中进行打印。
使用列表创建一个DataFrame:
我们可以使用list轻松在Pandas中创建一个DataFrame。
# importing the pandas library
import pandas as pd
# a list of strings
x = ['Python', 'Pandas']
# Calling DataFrame constructor on list
df = pd.DataFrame(x)
print(df)
输出
0
0 Python
1 Pandas
说明:在上面的代码中, 我们定义了一个名为” x”的变量, 它由字符串值组成。正在调用DataFrame构造函数以获取列表以打印值。
从ndarrays / List的Dict创建一个DataFrame
# importing the pandas library
import pandas as pd
info = {'ID' :[101, 102, 103], 'Department' :['B.Sc', 'B.Tech', 'M.Tech', ]}
df = pd.DataFrame(info)
print (df)
输出
ID Department
0 101 B.Sc
1 102 B.Tech
2 103 M.Tech
说明:在上面的代码中, 我们定义了一个名为” info”的字典, 该字典包含ID和Department的列表。为了打印值, 我们必须通过名为df的变量调用信息字典, 并将其作为参数传递给print()。
从系列字典创建数据框:
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']), 'two' : pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])}
d1 = pd.DataFrame(info)
print (d1)
输出
one two
a 1.0 1
b 2.0 2
c 3.0 3
d 4.0 4
e 5.0 5
f 6.0 6
g NaN 7
h NaN 8
说明:在上面的代码中, 名为” info”的词典由两个具有各自索引的Series组成。为了打印值, 我们必须通过名为d1的变量调用信息字典, 并将其作为参数传递给print()。
列选择
我们可以从DataFrame中选择任何列。这是演示如何从DataFrame中选择列的代码。
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']), 'two' : pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])}
d1 = pd.DataFrame(info)
print (d1 ['one'])
输出
a 1.0
b 2.0
c 3.0
d 4.0
e 5.0
f 6.0
g NaN
h NaN
Name: one, dtype: float64
说明:在上面的代码中, 名为” info”的字典由两个具有各自索引的Series组成。后来, 我们通过变量d1调用了信息字典, 并通过将其传递到print()从DataFrame中选择了”一个”系列。
列添加
我们还可以将任何新列添加到现有DataFrame中。以下代码演示了如何将任何新列添加到现有DataFrame中:
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']), 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
df = pd.DataFrame(info)
# Add a new column to an existing DataFrame object
print ("Add new column by passing series")
df['three']=pd.Series([20, 40, 60], index=['a', 'b', 'c'])
print (df)
print ("Add new column using existing DataFrame columns")
df['four']=df['one']+df['three']
print (df)
输出
Add new column by passing series
one two three
a 1.0 1 20.0
b 2.0 2 40.0
c 3.0 3 60.0
d 4.0 4 NaN
e 5.0 5 NaN
f NaN 6 NaN
Add new column using existing DataFrame columns
one two three four
a 1.0 1 20.0 21.0
b 2.0 2 40.0 42.0
c 3.0 3 60.0 63.0
d 4.0 4 NaN NaN
e 5.0 5 NaN NaN
f NaN 6 NaN NaN
说明:在上面的代码中, 名为f的字典由两个具有各自索引的Series组成。后来, 我们通过变量df调用了信息字典。
为了向现有DataFrame对象添加新列, 我们传递了一个新系列, 其中包含一些有关其索引的值, 并使用print()打印了其结果。
我们可以使用现有的DataFrame添加新列。添加了”四”列, 该列存储两列即一列和三列相加的结果。
列删除
我们还可以从现有DataFrame中删除任何列。此代码有助于说明如何从现有DataFrame中删除该列:
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2], index= ['a', 'b']), 'two' : pd.Series([1, 2, 3], index=['a', 'b', 'c'])}
df = pd.DataFrame(info)
print ("The DataFrame:")
print (df)
# using del function
print ("Delete the first column:")
del df['one']
print (df)
# using pop function
print ("Delete the another column:")
df.pop('two')
print (df)
输出
The DataFrame:
one two
a 1.0 1
b 2.0 2
c NaN 3
Delete the first column:
two
a 1
b 2
c 3
Delete the another column:
Empty DataFrame
Columns: []
Index: [a, b, c]
说明:
在上面的代码中, df变量负责调用信息字典并打印字典的全部值。我们可以使用delete或pop函数从DataFrame中删除列。
在第一种情况下, 我们使用了delete函数从DataFrame中删除”一个”列, 而在第二种情况下, 我们使用了pop函数从数据帧中删除了” two”列。
行选择, 添加和删除
行选择:
我们可以随时轻松地选择, 添加或删除任何行。首先, 我们将了解行的选择。让我们看看如何使用以下不同方式选择行:
按标签选择:
我们可以通过将行标签传递给loc函数来选择任何行。
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']), 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
df = pd.DataFrame(info)
print (df.loc['b'])
输出
one 2.0
two 2.0
Name: b, dtype: float64
说明:在上面的代码中, 名为info的字典由两个具有各自索引的Series组成。
为了选择行, 我们将行标签传递给loc函数。
通过整数位置选择:
也可以通过将整数位置传递给iloc函数来选择行。
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']), 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
df = pd.DataFrame(info)
print (df.iloc[3])
输出
one 4.0
two 4.0
Name: d, dtype: float64
说明:说明:在上面的代码中, 我们定义了一个名为info的字典, 该字典由两个具有各自索引的Series组成。
为了选择一行, 我们将整数位置传递给了iloc函数。
切片行
这是使用’:’运算符选择多行的另一种方法。
# importing the pandas library
import pandas as pd
info = {'one' : pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']), 'two' : pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f'])}
df = pd.DataFrame(info)
print (df[2:5])
输出
one two
c 3.0 3
d 4.0 4
e 5.0 5
说明:在上面的代码中, 我们为选择行定义了一个2:5的范围, 然后将其值打印在控制台上。
行加法:
我们可以使用append函数轻松地向DataFrame添加新行。它在末尾添加新行。
# importing the pandas library
import pandas as pd
d = pd.DataFrame([[7, 8], [9, 10]], columns = ['x', 'y'])
d2 = pd.DataFrame([[11, 12], [13, 14]], columns = ['x', 'y'])
d = d.append(d2)
print (d)
输出
x y
0 7 8
1 9 10
0 11 12
1 13 14
说明:在上面的代码中, 我们定义了两个单独的列表, 其中包含一些行和列。这些列已使用append函数添加, 然后结果显示在控制台上。
删除行:
我们可以使用索引标签从DataFrame删除或删除任何行。如果万一标签重复, 则将删除多行。
# importing the pandas library
import pandas as pd
a_info = pd.DataFrame([[4, 5], [6, 7]], columns = ['x', 'y'])
b_info = pd.DataFrame([[8, 9], [10, 11]], columns = ['x', 'y'])
a_info = a_info.append(b_info)
# Drop rows with label 0
a_info = a_info.drop(0)
输出
x y
1 6 7
1 10 11
说明:在上面的代码中, 我们定义了两个单独的列表, 其中包含一些行和列。
在这里, 我们定义了需要从列表中删除的行的索引标签。
DataFrame函数
DataFrame中使用了许多功能, 如下所示:
Functions | Description |
---|---|
Pandas DataFrame.append() | 将其他数据框的行添加到给定数据框的末尾。 |
Pandas DataFrame.apply() | 允许用户传递函数并将其应用于Pandas系列的每个单个值。 |
Pandas DataFrame.assign() | 将新列添加到数据框。 |
Pandas DataFrame.astype() | 将Pandas对象转换为指定的dtype.astype()函数。 |
Pandas DataFrame.concat() | 沿DataFrame中的轴执行串联操作。 |
Pandas DataFrame.count() | 计算每一列或每一行的非NA单元数。 |
Pandas DataFrame.describe() | 计算一些统计数据, 例如Series或DataFrame的数值的百分位数, 均值和标准差。 |
Pandas DataFrame.drop_duplicates() | 从DataFrame中删除重复的值。 |
Pandas DataFrame.groupby() | 将数据分成不同的组。 |
Pandas DataFrame.head() | 根据位置返回对象的前n行。 |
Pandas DataFrame.hist() | 将数值变量中的值分成” bins”。 |
Pandas DataFrame.iterrows() | 将行迭代为(索引, 系列)对。 |
Pandas DataFrame.mean() | 返回所请求轴的值的平均值。 |
Pandas DataFrame.melt() | 将DataFrame从宽格式取消转为长格式。 |
Pandas DataFrame.merge() | 将两个数据集合并为一个。 |
Pandas DataFrame.pivot_table() | 使用诸如求和, 计数, 平均值, 最大值和最小值之类的计算来聚合数据。 |
Pandas DataFrame.query() | 过滤数据框。 |
Pandas DataFrame.sample() | 从数据框中随机选择行和列。 |
Pandas DataFrame.shift() | 移位列或从数据帧中减去具有上一行值的列值。 |
Pandas DataFrame.sort() | 对数据框进行排序。 |
Pandas DataFrame.sum() | 返回用户请求的轴的值的总和。 |
Pandas DataFrame.to_excel() | 将数据框导出到excel文件。 |
Pandas DataFrame.transpose() | 转置数据框的索引和列。 |
Pandas DataFrame.where() | 检查数据框是否存在一种或多种情况。 |
评论前必须登录!
注册