数据分析之pandas(进阶)

1.合并pd.merge

pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
         left_index=False, right_index=False, sort=True,
         suffixes=('_x', '_y'), copy=True, indicator=False,
         validate=None)
  • left: 拼接的左侧DataFrame对象
  • right: 拼接的右侧DataFrame对象
  • on: 要加入的列或索引级别名称。 必须在左侧和右侧DataFrame对象中找到。 如果未传递且left_index和right_index为False,则DataFrame中的列的交集将被推断为连接键。
  • left_on:左侧DataFrame中的列或索引级别用作键。 可以是列名,索引级名称,也可以是长度等于DataFrame长度的数组。
  • right_on: 左侧DataFrame中的列或索引级别用作键。 可以是列名,索引级名称,也可以是长度等于DataFrame长度的数组。
  • left_index: 如果为True,则使用左侧DataFrame中的索引(行标签)作为其连接键。 对于具有MultiIndex(分层)的DataFrame,级别数必须与右侧DataFrame中的连接键数相匹配。
  • right_index: 与left_index功能相似。
  • how: One of ‘left’, ‘right’, ‘outer’, ‘inner’. 默认inner。inner是取交集,outer取并集。比如left:[‘A’,‘B’,‘C’];right[’'A,‘C’,‘D’];inner取交集的话,left中出现的A会和right中出现的买一个A进行匹配拼接,如果没有是B,在right中没有匹配到,则会丢失。'outer’取并集,出现的A会进行一一匹配,没有同时出现的会将缺失的部分添加缺失值。
  • sort: 按字典顺序通过连接键对结果DataFrame进行排序。 默认为True,设置为False将在很多情况下显着提高性能。
  • suffixes: 用于重叠列的字符串后缀元组。 默认为(‘x’,’ y’)。
  • copy: 始终从传递的DataFrame对象复制数据(默认为True),即使不需要重建索引也是如此。
  • indicator:将一列添加到名为_merge的输出DataFrame,其中包含有关每行源的信息。 _merge是分类类型,并且对于其合并键仅出现在“左”DataFrame中的观察值,取得值为left_only,对于其合并键仅出现在“右”DataFrame中的观察值为right_only,并且如果在两者中都找到观察点的合并键,则为left_only。

2.去除空数据

pandas的设计目标之一就是使得处理缺失数据的任务更加轻松些。pandas使用NaN作为缺失数据的标记。
dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
效果同布尔表达式df[df.notnull()]

df = pd.DataFrame({
    
    "name": ['Alfred', 'Batman', 'Catwoman'],
                   "toy": [np.nan, 'Batmobile', 'Bullwhip'],
                   "born": [pd.NaT, pd.Timestamp("1940-04-25"), pd.NaT]})
#
#           name        toy       born
#    0    Alfred        NaN        NaT
#    1    Batman  Batmobile 1940-04-25
#    2  Catwoman   Bullwhip        NaT

df.dropna()
#        name        toy       born
#   1  Batman  Batmobile 1940-04-25

3.去除重复

drop_duplicates(subset=' 列名',keep='firsrt',inplace='True')函数是删除DataFrame的某列中重复项的函数。

subset,输入列名,形式为subset=‘列名1’,可输入多列,形式为subset=[‘列名1’,‘列名2’]

keep包括’first’,‘last’,False,三个参数,注意first和last带引号,而False没有,'first’是保留重复项中第一个,last是保留最后一个,False是都不保留

import pandas as pd
dict={
    
    'x':[1,2,3,6],'y':[1,4,1,1],'z':[1,2,4,1]}
df=pd.DataFrame(dict)
print(df, "\n")
#    x  y  z
# 0  1  1  1
# 1  2  4  2
# 2  3  1  4
# 3  6  1  1 

df.drop_duplicates(subset=['y','z'],keep='first',inplace=True)
print(df)
#   x  y  z
# 0  1  1  1
# 1  2  4  2
# 2  3  1  4

4.apply数据处理

用于处理数据,类似于python中的apply函数,返回迭代器
apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)

df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
print(df)
#       A  B
#    0  4  9
#    1  4  9
#    2  4  9

df.apply(np.sqrt)
#         A    B
#    0  2.0  3.0
#    1  2.0  3.0
#    2  2.0  3.0

5.数据聚合

groupby(by=None, 
		axis=0, 
		level=None, 
		as_index: bool = True, 
		sort: bool = True,
		group_keys: bool = True, 
		squeeze: bool = False, 
		observed: bool = False)

6.根据某一列排序

https://blog.csdn.net/qq1483661204/article/details/79824381

7. 修改某一列类型

https://blog.csdn.net/zgljl2012/article/details/54880353

8. 其他

https://zhuanlan.zhihu.com/p/142972462

http://localhost:8888/notebooks/Desktop/zsw/code/jupyter_notebook/pandas%E8%BF%9B%E9%98%B6.ipynb

猜你喜欢

转载自blog.csdn.net/u014665013/article/details/114792091