气泡水 发表于 2021-12-21 16:06:52

20 个好用的 pandas 函数!(上)


  今天分享几个pandas函数,大家可能平时看到的不多,但是使用起来倒是非常的方便,也能够帮助我们数据分析人员大幅度地提高工作效率。

  1、items()方法

  pandas当中的items()方法可以用来遍历数据集当中的每一列,同时返回列名以及每一列当中的内容,通过以元组的形式,示例如下

df = pd.DataFrame({'species': ['bear', 'bear', 'marsupial'],
                  'population': },
                  index=['panda', 'polar', 'koala'])
df

  output

       speciespopulation
panda       bear      1864
polar       bear       22000
koalamarsupial       80000

  然后我们使用items()方法

for label, content in df.items():
    print(f'label: {label}')
    print(f'content: {content}', sep='\n')
    print("=" * 50)

  output

label: species
content: panda         bear
polar         bear
koala    marsupial
Name: species, dtype: object
==================================================
label: population
content: panda   1864
polar    22000
koala    80000
Name: population, dtype: int64
==================================================

  相继的打印出了‘species’和‘population’这两列的列名和相应的内容

  2、iterrows()方法

  而对于iterrows()方法而言,其功能则是遍历数据集当中的每一行,返回每一行的索引以及带有列名的每一行的内容,示例如下

for label, content in df.iterrows():
    print(f'label: {label}')
    print(f'content: {content}', sep='\n')
    print("=" * 50)
      
       output

label: panda
content: species       bear
population    1864
Name: panda, dtype: object
==================================================
label: polar
content: species      bear
population    22000
Name: polar, dtype: object
==================================================
label: koala
content: species       marsupial
population      80000
Name: koala, dtype: object
==================================================

  3、insert()方法

  insert()方法主要是用于在数据集当中的特定位置处插入数据,示例如下

df.insert(1, "size", )

  output

   speciessizepopulation
panda       bear2000      1864
polar       bear3000       22000
koalamarsupial4000       80000

  可见在DataFrame数据集当中,列的索引也是从0开始的

  4、assign()方法

  assign()方法可以用来在数据集当中添加新的列,示例如下

df.assign(size_1=lambda x: x.population * 9 / 5 + 32)

  output

      speciespopulation    size_1
panda       bear      1864    3387.2
polar       bear       22000   39632.0
koalamarsupial       80000144032.0

  从上面的例子中可以看出,我们通过一个lambda匿名函数,在数据集当中添加一个新的列,命名为‘size_1’,当然我们也可以通过assign()方法来创建不止一个列

df.assign(size_1 = lambda x: x.population * 9 / 5 + 32,
          size_2 = lambda x: x.population * 8 / 5 + 10)

  output

    speciespopulation    size_1    size_2
panda       bear      1864    3387.2    2992.4
polar       bear       22000   39632.0   35210.0
koalamarsupial       80000144032.0128010.0

  5、eval()方法

  eval()方法主要是用来执行用字符串来表示的运算过程的,例如

df.eval("size_3 = size_1 + size_2")

  output

   speciespopulation    size_1    size_2    size_3
panda       bear      1864    3387.2    2992.4    6379.6
polar       bear       22000   39632.0   35210.0   74842.0
koalamarsupial       80000144032.0128010.0272042.0

  当然我们也可以同时对执行多个运算过程

df = df.eval('''
size_3 = size_1 + size_2
size_4 = size_1 - size_2
''')

  output

    speciespopulation    size_1    size_2    size_3   size_4
panda       bear      1864    3387.2    2992.4    6379.6    394.8
polar       bear       22000   39632.0   35210.0   74842.0   4422.0
koalamarsupial       80000144032.0128010.0272042.016022.0

  6、pop()方法

  pop()方法主要是用来删除掉数据集中特定的某一列数据

df.pop("size_3")

  output

panda      6379.6
polar   74842.0
koala    272042.0
Name: size_3, dtype: float64

  而原先的数据集当中就没有这个‘size_3’这一例的数据了

  7、truncate()方法

  truncate()方法主要是根据行索引来筛选指定行的数据的,示例如下

df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'],
                   'B': ['f', 'g', 'h', 'i', 'j'],
                   'C': ['k', 'l', 'm', 'n', 'o']},
                  index=)

  output

   ABC
1afk
2bgl
3chm
4din
5ejo

  我们使用truncate()方法来做一下尝试

df.truncate(before=2, after=4)

  output

   ABC
2bgl
3chm
4din

  我们看到参数before和after存在于truncate()方法中,目的就是把行索引2之前和行索引4之后的数据排除在外,筛选出剩余的数据

  8、count()方法

  count()方法主要是用来计算某一列当中非空值的个数,示例如下

df = pd.DataFrame({"Name": ["John", "Myla", "Lewis", "John", "John"],
                   "Age": ,
                   "Single": })

  output

   Name   Age Single
0   John24.0   True
1   Myla   NaN   True
2Lewis25.0    NaN
3   John33.0   True
4   John26.0False

  我们使用count()方法来计算一下数据集当中非空值的个数

df.count()

  output

Name      5
Age       4
Single    4
dtype: int64

  9、add_prefix()方法/add_suffix()方法

  add_prefix()方法和add_suffix()方法分别会给列名以及行索引添加后缀和前缀,对于Series()数据集而言,前缀与后缀是添加在行索引处,而对于DataFrame()数据集而言,前缀与后缀是添加在列索引处,示例如下

s = pd.Series()

  output

0    1
1    2
2    3
3    4
dtype: int64

  我们使用add_prefix()方法与add_suffix()方法在Series()数据集上

s.add_prefix('row_')

  output

row_0    1
row_1    2
row_2    3
row_3    4
dtype: int64

  又例如

s.add_suffix('_row')

  output

0_row    1
1_row    2
2_row    3
3_row    4
dtype: int64

  而对于DataFrame()形式数据集而言,add_prefix()方法以及add_suffix()方法是将前缀与后缀添加在列索引处的

df = pd.DataFrame({'A': , 'B': })

  output

   AB
013
124
235
346
示例如下

  示例如下

df.add_prefix("column_")

  output

column_Acolumn_B
0         1         3
1         2         4
2         3         5
3         4         6

  又例如

df.add_suffix("_column")

  output

   A_columnB_column
0         1         3
1         2         4
2         3         5
3         4         6

  【免责声明】本文系转载,来源于关于数据分析与可视化 ,作者俊欣 。转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与联系我们,我们会予以更改或删除相关文章,以保证您的权益!

页: [1]
查看完整版本: 20 个好用的 pandas 函数!(上)