Pandas 基础知识笔记

整理一下 pandas 的笔记,为的是记住这些pandas的常用工具。

pandas中数据结构有一维的 Series,二维的 DataFrame 和三维的

常用操作

读取数据

# 查看列名列表
ratings.columns

# Index(['userId', 'movieId', 'rating', 'timestamp'], dtype='object')

# 查看索引列
ratings.index

# RangeIndex(start=0, stop=100836, step=1)

# 查看每列的数据类型
ratings.dtypes

"""
userId         int64
movieId        int64
rating       float64
timestamp      int64
dtype: object
"""

读取csv文件,自行指定分隔符

fpath = "./datas/crazyant/access_pvuv.txt"
pvuv = pd.read_csv(
    fpath,
    sep="\t",
    header=None, # header参数的含义为
    names=['pdate', 'pv', 'uv'] # names用于设置字段的名称
)

pandas数据结构

Series和DataFrame简单介绍

查询数据

df.loc 方法,根据行、列的标签值查询。

df.iloc 方法,根据行、列的数索引查询。

具体见博客查询数据

pandas 将时间戳转化为日期的方法:

pd.to_datetime(df['timestamp'], unit='s')

参考自:https://www.cnblogs.com/rainduck/p/5914471.html

concat() 方法:用于合并表格。
参见 https://zhuanlan.zhihu.com/p/69224745

2021-02-06
df.dropna(thresh=n)
thresh:(打稻谷)脱粒
这一行除去NA值,剩余数值的数量大于等于n,便显示这一行。

army.set_index(column) 将某列设为行索引。

size(): 与 groupby() 连用时,返回分组中字段的个数。

regiment.groupby(['regiment', 'company']).size()
posted @ 2021-02-04 22:15  TheByteMan  阅读(148)  评论(0)    收藏  举报