Python学习笔记(一):Pandas中的数据结构

本文介绍了Python的Pandas库中的三种主要数据结构:Series、DataFrame和Panel,它们基于Numpy构建。重点讲解了Series和DataFrame的创建,以及DataFrame的增删行/列操作和数据查询方法,包括loc、iloc和ix以及布尔索引。
摘要由CSDN通过智能技术生成

Pandas中的数据结构:

  1. Series,序列,大小不可变;
  2. DataFrame,数据帧
  3. Panel,面板

这三种数据结构都是基于Numpy数组构建的,除Series外,其他两种大小可变。

 

创建Series示例:

>>> s = pd.Series([1, 3, 5, np.nan, 6])

创建DataFrame示例: 

>>> dates = pd.date_range('20170101', periods = 7)
>>> df = pd.DataFrame(np.random.randn(7, 4), index = dates, columns = list('ABCD'))
>>> df
                   A         B         C         D
2017-01-01  0.026631 -1.123369 -0.593639 -0.450369
2017-01-02  0.381743  1.069795 -0.521409 -1.688327
2017-01-03  0.173431  1.858795  0.702816  0.338846
2017-01-04  0.497656 -1.271299 -0.698368  0.106706
2017-01-05  1.521118 -0.622020  0.407636  1.247326
2017-01-06 -2.562246 -1.194964 -1.659602 -0.038506
2017-01-07  1.098590  0.422019  0.416406  0.088594
>>>
>>> pd.DataFrame([1,2,3,4,5])  # 生成5行1列的df
   0
0  1
1  2
2  3
3  4
4  5
>>>
>>> pd.DataFrame([['Alex', 10], ['Bob',12], ['Clarke', 13]])  # 3行2列
        0   1
0    Alex  10
1     Bob  12
2  Clarke  13

也就是说,最外层的' [ ] ' 括住的是列,里层的 ' [ ] ' 是一行,外层的' [ ] '括住了多少个成员,就有多少行。

 

DataFrame增删行/列的操作:

>>> # 添加新列
>>> df['new'] = ...

>>> # 删除列
>>> del df['one']
>>> df.pop('two')

>>> # 附加新的行
>>> df = df.append(df_2, ignore_index=True)

>>> # 删除行
>>> df = df.drop(0)  # 0表示index

这里注意append()方法的notes:
    Notes
    -----
    Iteratively appending rows to a DataFrame can be more computationally
    intensive than a single concatenate. A better solution is to append
    those rows to a list and then concatenate the list with the original
    DataFrame all at once. 先把rows整合成一个list,再一起添加到df中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值