Pandas中的方法及使用示例(三)

简介: Pandas中的方法及使用示例(三)

header – 是否保留列名

默认取值为 True,即保留列名。

data = pd.read_csv('./数据文件/距离最小的三个点.csv', sep='#')
print(data)
data.to_excel(
  './数据文件/data2.xlsx',
  sheet_name='data2',
  header=False
)

index – 是否保留行索引

默认取值为 True,即保留行索引。

data = pd.read_csv('./数据文件/距离最小的三个点.csv', sep='#')
print(data)
data.to_excel(
  './数据文件/data2.xlsx',
  sheet_name='data2',
  header=False,
  index=False
)

7. merge() – 表格的连接

left、right – 指定左表右表

l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([4, 3, 6], index=['a', 'b', 'e']),
  pd.Series([4, 2, 6], index=['a', 'b', 'e'])
]
df2 = pd.DataFrame(l2)
print(pd.merge(df1, df2))

两个表中都 b 列,根据 b 列进行内连接。连接后重复列保留一个

how – 连接方式

使用 how 指定连接方式。

  • left:左连接
  • right:右连接
  • outer:外连接
  • inner:内连接(默认)
l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([4, 3, 6], index=['a', 'b', 'e']),
  pd.Series([4, 2, 6], index=['a', 'b', 'e'])
]
df2 = pd.DataFrame(l2)
print(pd.merge(df1, df2, how='outer'))

on – 指定连接依据的列名

两个表连接时,依据的列的列名相同时,使用 on 指定连接所依据的列名。

l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([4, 3, 6], index=['a', 'b', 'e']),
  pd.Series([4, 2, 6], index=['a', 'b', 'e'])
]
df2 = pd.DataFrame(l2)
print(pd.merge(df1, df2, how='outer', on='b'))

left_on、right_on – 左右表连接时各自依据的列名

  • left_on:连接时,左表所依据的列的列名
  • right_on:连接时,右表所依据的列的列名
l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([1, 3, 6], index=['a', 'b', 'e']),
  pd.Series([1, 2, 6], index=['a', 'b', 'e'])
]
df2 = pd.DataFrame(l2)
print(
  pd.merge(
    df1, 
    df2, 
    how='outer', 
    left_on='f', 
    right_on='a'
  )
)

suffixes – 为左右表中重复列名定义后缀

使用 suffixes 为左右表中重复列名定义后缀,使得连接两个表后可以区分相同列名的列来自左表还是右表。

l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([1, 3, 6], index=['a', 'b', 'e']),
  pd.Series([1, 2, 6], index=['a', 'b', 'e'])
]
df2 = pd.DataFrame(l2)
print(
  pd.merge(
    df1, 
    df2, 
    how='outer', 
    left_on='f', 
    right_on='a',
    suffixes=('1', '2')
  )
)

8. concat() – 表格的拼接

concat() 一般用于表格的纵向拼接。

axis – 拼接的方向

  • axis = 0:纵向拼接表(默认)
l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([1, 3, 6], index=['a', 'b', 'e']),
  pd.Series([1, 2, 6], index=['a', 'b', 'e'])
]
df2 = pd.DataFrame(l2)
print(
  pd.concat([df1, df2])
)

列索引不一致

l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([1, 3, 6], index=['f', 'b', 'c']),
  pd.Series([1, 2, 6], index=['f', 'b', 'c'])
]
df2 = pd.DataFrame(l2)
print(
  pd.concat([df1, df2])
)

  • axis = 1:横向拼接表
l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([1, 3, 6], index=['f', 'b', 'c']),
  pd.Series([1, 2, 6], index=['f', 'b', 'c'])
]
df2 = pd.DataFrame(l2)
print(
  pd.concat([df1, df2], axis=1)
)

l1 = [
  pd.Series([1, 2, 3], index=['f', 'b', 'c']),
  pd.Series([1, 2, 3], index=['f', 'b', 'c'])
]
df1 = pd.DataFrame(l1)
l2 = [
  pd.Series([1, 3, 6], index=['f', 'b', 'c']),
  pd.Series([1, 2, 6], index=['f', 'b', 'c'])
]
df2 = pd.DataFrame(l2)
df2.index = [2,3]
print(
  pd.concat([df1, df2], axis=1)
)

行索引不一致

9. get_dummies() – 对离散数据进行独热编码

对表中的取值为离散数据的列进行独热编码,将离散类型的信息转化为使用独热编码进行表示的形式。

data – 需要进行处理的表

l1 = [
  pd.Series(['a', 12], index=['f', 'g']),
  pd.Series(['b', 11], index=['f', 'g']),
  pd.Series(['a', 12], index=['f', 'g'])
]
df1 = pd.DataFrame(l1)
print(
  pd.get_dummies(df1)
)

f 列中的数据为离散型的数据,可以使用 get_dummies() 对该列进行独热编码。

prefix – 设置列名的前缀

使用 prefix 设置进行独热编码的列的列名的前缀,默认值为 None

l1 = [
  pd.Series(['a', 12], index=['f', 'g']),
  pd.Series(['b', 11], index=['f', 'g']),
  pd.Series(['a', 12], index=['f', 'g'])
]
df1 = pd.DataFrame(l1)
print(
  pd.get_dummies(df1, prefix='hello_')
)


相关文章
|
1月前
|
数据采集 机器学习/深度学习 Python
【机器学习】数据清洗——基于Pandas库的方法删除重复点
【机器学习】数据清洗——基于Pandas库的方法删除重复点
137 1
|
1月前
|
人工智能 安全 数据挖掘
Pandas AI:Pandas与人工智能的结合,让你不再拘泥于如何使用pandas方法及处理语法
Pandas AI:Pandas与人工智能的结合,让你不再拘泥于如何使用pandas方法及处理语法
123 1
|
1月前
|
数据挖掘 索引 Python
在Pandas中通过时间频率来汇总数据的三种常用方法
在Pandas中通过时间频率来汇总数据的三种常用方法
87 0
|
1月前
|
SQL 索引 Python
Pandas Query 方法深度总结
Pandas Query 方法深度总结
|
9月前
|
机器学习/深度学习 算法 物联网
时间序列的重采样和pandas的resample方法介绍
重采样是时间序列分析中处理时序数据的一项基本技术。它是关于将时间序列数据从一个频率转换到另一个频率,它可以更改数据的时间间隔,通过上采样增加粒度,或通过下采样减少粒度。在本文中,我们将深入研究Pandas中重新采样的关键问题。
91 1
|
1月前
|
Python
使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序
【5月更文挑战第2天】使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序。示例代码展示了如何按'Name'和'Age'列排序 DataFrame。先按'Name'排序,再按'Age'排序。sort_values()的by参数接受列名列表,ascending参数控制排序顺序(默认升序),inplace参数决定是否直接修改原DataFrame。
41 1
|
1月前
|
数据可视化 数据挖掘 C++
数据分析综合案例讲解,一文搞懂Numpy,pandas,matplotlib,seaborn技巧方法
数据分析综合案例讲解,一文搞懂Numpy,pandas,matplotlib,seaborn技巧方法
|
1月前
|
数据处理 Python
使用Pandas解决问题:对比两列数据取最大值的五种方法
​在数据处理和分析中,经常需要比较两个或多个列的值,并取其中的最大值。Pandas库作为Python中数据处理和分析的强大工具,提供了多种灵活的方法来实现这一需求。本文将详细介绍五种使用Pandas对比两列数据并取最大值的方法,通过代码示例和案例分析,帮助新手更好地理解并掌握这些技巧。
46 0
|
1月前
|
存储 数据可视化 数据挖掘
Pandas 28种常用方法使用总结(下)
Pandas 28种常用方法使用总结
|
1月前
|
数据挖掘 Serverless 数据处理
Pandas 28种常用方法使用总结(上)
Pandas 28种常用方法使用总结