栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

记录用pandas遇到的一些问题

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记录用pandas遇到的一些问题

pandas学习笔记
  • pandas.Series.map
    • 举例说明
      • 字典映射
    • 函数映射
  • pandas中的apply()函数
    • pandas.Dataframe.apply
    • Series.apply
  • 判断两个dataframe或者series是否相同
    • pandas.Dataframe.equals
    • pandas.Series.equals
  • pandas.Series.str.translate函数
  • 举例说明

pandas.Series.map

map()函数可以用于Series对象或Dataframe对象的一列,接收函数或字典对象作为参数,返回经过函数或字典映射处理后的值。

用法:
Series.map(arg, na_action=None)

  • Map values of Series according to input correspondence.
  • Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.

参数:

  • argfunction, collections.abc.Mapping subclass or Series
    Mapping correspondence.
  • na_action{None, ‘ignore’}, default None
    If ‘ignore’, propagate NaN values, without passing them to the mapping correspondence.

返回:

  • Series
    Same index as caller.

官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html

举例说明
data = pd.Series(['PC - Garnitury/Nausniki','Aksessuary - PS2','Aksessuary - PS3','Aksessuary - PS4','Aksessuary - PSP'])

输出:

0    PC - Garnitury/Nausniki
1           Aksessuary - PS2
2           Aksessuary - PS3
3           Aksessuary - PS4
4           Aksessuary - PSP
dtype: object
字典映射
new_data = data.map({'PC - Garnitury/Nausniki':'A','Aksessuary - PS2':'B','Aksessuary - PS3':'C','Aksessuary - PS4':'D','Aksessuary - PSP':'E'})

输出:

0    A
1    B
2    C
3    D
4    E
dtype: object

注意:Series.map()函数生成了一个原来Series的一个副本,并不改变原来data的值:

print(data)

输出:

0    PC - Garnitury/Nausniki
1           Aksessuary - PS2
2           Aksessuary - PS3
3           Aksessuary - PS4
4           Aksessuary - PSP
dtype: object
函数映射
data.map(lambda x:x.split('-'))

输出:

0    [PC ,  Garnitury/Nausniki]
1           [Aksessuary ,  PS2]
2           [Aksessuary ,  PS3]
3           [Aksessuary ,  PS4]
4           [Aksessuary ,  PSP]
dtype: object
pandas中的apply()函数

pandas中的apply()用于调用一个函数,它的作用对象是可以是Series,也可以是Dataframe。

pandas.Dataframe.apply

语法:

Dataframe.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)
Apply a function along an axis of the Dataframe.
Objects passed to the function are Series objects whose index is either the Dataframe’s index (axis=0) or the Dataframe’s columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument.

官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Dataframe.apply.html#pandas.Dataframe.apply

Series.apply

Series.apply(func, convert_dtype=True, args=(), **kwargs)[source]
Invoke function on values of Series.
Can be ufunc (a NumPy function that applies to the entire Series) or a Python function that only works on single values.

官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.apply.html#pandas.Series.apply

从某种意义上来说,map函数的作用与apply函数类似。但是apply函数比map函数更灵活。

这两篇文章讲的不错:
知乎
知乎

判断两个dataframe或者series是否相同 pandas.Dataframe.equals

Dataframe.equals()函数查看两个Dataframe是否具有相同的形状和元素。

官方文档 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Dataframe.equals.html?highlight=equals#pandas.Dataframe.equals

pandas.Series.equals

Series.equals()函数查看两个Series是否具有相同的形状和元素。

官方文档 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.equals.html?highlight=equals#pandas.Series.equals

pandas.Series.str.translate函数

pandas.Series.str.translate使用转换表根据转换表转换字符串的调用方系列。如果要翻译的值不止一个,则将字典传递给maketrans函数以创建翻译表。

官方文档 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.translate.html?highlight=translate#pandas.Series.str.translate

举例说明
data = pd.Dataframe(['Книги - Открытки','Книги - Познавательная литература','Книги - Путеводители','Книги - Художественная литература'],columns=['eyu'])
data

输出:

0                       Книги - Открытки
1      Книги - Познавательная литература
2                   Книги - Путеводители
3      Книги - Художественная литература
dtype: object

将俄语转换成英语:

symbols=(u"абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ", u"abvgdeejzijklmnoprstufhzcss_y_euaABVGDEEJZIJKLMNOPRSTUFHZCSS_Y_EUA")
english = {ord(a):ord(b) for a, b in zip(*symbols)}
data['english'] = data.eyu.str.translate(english)
print(data)

输出:

                                 eyu                            english
0                   Книги - Открытки                   Knigi - Otkrytki
1  Книги - Познавательная литература  Knigi - Poznavatel_naa literatura
2               Книги - Путеводители               Knigi - Putevoditeli
3  Книги - Художественная литература  Knigi - Hudojestvennaa literatura
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/293915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号