pandas <十分钟掌握pandas常用操作>

panda version:’0.19.2’

1.创建数据集

In [58]: names=['Bob','Jessica','Mary','John','Mel']

In [59]: births = [968, 155, 77, 578, 973]

In [60]: BabyDataSet = list(zip(names,births))
    ...: BabyDataSet
Out[60]: [('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]

In [61]: df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births
    ...: '])
    ...: df
Out[61]:
     Names  Births
0      Bob     968
1  Jessica     155
2     Mary      77
3     John     578
4      Mel     973

2.查看数据类型

In [64]: df.dtypes
Out[64]:
Names     object
Births     int64
dtype: object

In [65]: df.Names.dtypes
Out[65]: dtype('O')

In [66]: df.Births.dtypes
Out[66]: dtype('int64')

3.按照指定 的一列排序

In [68]: df.sort_values(['Births'])
Out[68]:
     Names  Births
2     Mary      77
1  Jessica     155
3     John     578
0      Bob     968
4      Mel     973

In [69]: df.sort_values(['Births'],ascending=False)
Out[69]:
     Names  Births
4      Mel     973
0      Bob     968
3     John     578
1  Jessica     155
2     Mary      77

4.选取前几行或者末尾几行

In [72]: df.head(3)
Out[72]:
     Names  Births
0      Bob     968
1  Jessica     155
2     Mary      77

In [73]: df.tail(2)
Out[73]:
  Names  Births
3  John     578
4   Mel     973

In [74]: df
Out[74]:
     Names  Births
0      Bob     968
1  Jessica     155
2     Mary      77
3     John     578
4      Mel     973

5.选取一列的最大值

In [75]: df['Births']
Out[75]:
0    968
1    155
2     77
3    578
4    973
Name: Births, dtype: int64

In [76]: df['Births'].max()
Out[76]: 973


In [77]: df['Names'][df['Births'] == df['Births'].max()]
Out[77]:
4    Mel
Name: Names, dtype: object
# encoding=utf-8
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt

# Creating a Series by passing a list of values, letting pandas create a default integer index
s = pd.Series([1, 3, 5, np.nan, 6, 8])
print(s)
# Creating a DataFrame by passing a numpy array, with a datetime index and labeled columns:
0    1.0
1    3.0
2    5.0
3    NaN
4    6.0
5    8.0
dtype: float64

dates = pd.date_range('20130101', periods=6)
print(dates)
# Creating a DataFrame by passing a numpy array, with a datetime index and labeled columns:
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
print(df)
df2 = pd.DataFrame({'A': 1.,
                    'B': pd.Timestamp('20130102'),
                    'C': pd.Series(1, index=list(range(4)), dtype='float32'),
                    'D': np.array([3] * 4, dtype='int32'),
                    'E': pd.Categorical(["test", "train", "test", "train"]),
                    'F': 'foo'})
print(df2)
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06'],
              dtype='datetime64[ns]', freq='D')
                   A         B         C         D
2013-01-01 -1.324399 -0.917851 -0.710650  0.977088
2013-01-02  0.034877 -0.417994  1.412711 -0.626197
2013-01-03 -0.559784 -0.085540  1.067182  0.649621
2013-01-04  0.849592 -1.251283  1.956991  1.189781
2013-01-05 -1.742392  0.193744 -0.570087 -0.277156
2013-01-06 -0.129934 -0.890113 -1.324529 -1.298726
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo
# 查看数据的头部和尾部
# See the top & bottom rows of the frame
print "--------*---------------"
print(df2.head(1))
print(df2.tail(1))
print(df2.values)

# 排序
# Sorting by an axis(轴)
print "--------*---------------"
# 可以看到按照列名排序了
print df
print df.sort_index(axis=1, ascending=False)
# Sorting by values
# 按照B列排序
print  df.sort_values(by='B')
     A          B    C  D     E    F
0  1.0 2013-01-02  1.0  3  test  foo
     A          B    C  D      E    F
3  1.0 2013-01-02  1.0  3  train  foo
[[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]
--------*---------------
                   A         B         C         D
2013-01-01 -1.324399 -0.917851 -0.710650  0.977088
2013-01-02  0.034877 -0.417994  1.412711 -0.626197
2013-01-03 -0.559784 -0.085540  1.067182  0.649621
2013-01-04  0.849592 -1.251283  1.956991  1.189781
2013-01-05 -1.742392  0.193744 -0.570087 -0.277156
2013-01-06 -0.129934 -0.890113 -1.324529 -1.298726
                   D         C         B         A
2013-01-01  0.977088 -0.710650 -0.917851 -1.324399
2013-01-02 -0.626197  1.412711 -0.417994  0.034877
2013-01-03  0.649621  1.067182 -0.085540 -0.559784
2013-01-04  1.189781  1.956991 -1.251283  0.849592
2013-01-05 -0.277156 -0.570087  0.193744 -1.742392
2013-01-06 -1.298726 -1.324529 -0.890113 -0.129934
                   A         B         C         D
2013-01-04  0.849592 -1.251283  1.956991  1.189781
2013-01-01 -1.324399 -0.917851 -0.710650  0.977088
2013-01-06 -0.129934 -0.890113 -1.324529 -1.298726
2013-01-02  0.034877 -0.417994  1.412711 -0.626197
2013-01-03 -0.559784 -0.085540  1.067182  0.649621
2013-01-05 -1.742392  0.193744 -0.570087 -0.277156

# Selecting a single column, which yields a Series, equivalent to df.A
print df.A
print df['A']
# Selecting via [], which slices the rows.
print df[0:3]
2013-01-01   -1.324399
2013-01-02    0.034877
2013-01-03   -0.559784
2013-01-04    0.849592
2013-01-05   -1.742392
2013-01-06   -0.129934
Freq: D, Name: A, dtype: float64
2013-01-01   -1.324399
2013-01-02    0.034877
2013-01-03   -0.559784
2013-01-04    0.849592
2013-01-05   -1.742392
2013-01-06   -0.129934
Freq: D, Name: A, dtype: float64
                   A         B         C         D
2013-01-01 -1.324399 -0.917851 -0.710650  0.977088
2013-01-02  0.034877 -0.417994  1.412711 -0.626197
2013-01-03 -0.559784 -0.085540  1.067182  0.649621
#选取第一行
print df.loc[dates[0]]
A   -1.324399
B   -0.917851
C   -0.710650
D    0.977088

Name: 2013-01-01 00:00:00, dtype: float64
#选取所有行中的A、B列
df.loc[:,['A','B']]
AB
2013-01-01-1.324399-0.917851
2013-01-020.034877-0.417994
2013-01-03-0.559784-0.085540
2013-01-040.849592-1.251283
2013-01-05-1.7423920.193744
2013-01-06-0.129934-0.890113
df.loc['20130102':'20130104',['A','B']]
#获取一个值
df.loc[dates[0],'A']
-1.3243992801327025
#Using the isin() method for filtering:
df2 = df.copy()
df2['E'] = ['one', 'one','two','three','four','three']
df2
ABCDE
2013-01-01-1.324399-0.917851-0.7106500.977088one
2013-01-020.034877-0.4179941.412711-0.626197one
2013-01-03-0.559784-0.0855401.0671820.649621two
2013-01-040.849592-1.2512831.9569911.189781three
2013-01-05-1.7423920.193744-0.570087-0.277156four
2013-01-06-0.129934-0.890113-1.324529-1.298726three
#Using the isin() method for filtering:
df2[df2['E'].isin(['two','four'])]
ABCDE
2013-01-03-0.559784-0.0855401.0671820.649621two
2013-01-05-1.7423920.193744-0.570087-0.277156four
s1 = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130102', periods=6))
s1
2013-01-02 1 2013-01-03 2 2013-01-04 3 2013-01-05 4 2013-01-06 5 2013-01-07 6 Freq: D, dtype: int64
#在原有的数据集上添加新的数据
df['F'] = s1
df
ABCDF
2013-01-01-1.324399-0.917851-0.7106500.977088NaN
2013-01-020.034877-0.4179941.412711-0.6261971.0
2013-01-03-0.559784-0.0855401.0671820.6496212.0
2013-01-040.849592-1.2512831.9569911.1897813.0
2013-01-05-1.7423920.193744-0.570087-0.2771564.0
2013-01-06-0.129934-0.890113-1.324529-1.2987265.0
# 每列的平均数
df.mean()
A -0.478673 B -0.561506 C 0.305270 D 0.102402 F 3.000000 dtype: float64
# 使用lamda表达式作用于每一列
df.apply(lambda x: x.max() - x.min())
A 2.591984 B 1.445027 C 3.281519 D 2.488508 F 4.000000 dtype: float64
# 字符串大小写转换
s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
s.str.lower()
0 a 1 b 2 c 3 aaba 4 baca 5 NaN 6 caba 7 dog 8 cat dtype: object
# merge操作
left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})
right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
left
right
pd.merge(left, right, on='key')
keylvalrval
0foo14
1foo15
2foo24
3foo25
left = pd.DataFrame({'key': ['foo', 'bar'], 'lval': [1, 2]})
right = pd.DataFrame({'key': ['foo', 'bar'], 'rval': [4, 5]})
left
right
pd.merge(left, right, on='key')
keylvalrval
0foo14
1bar25
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C' : np.random.randn(8),
'D' : np.random.randn(8)})
df
ABCD
0fooone0.850151-0.095071
1barone0.2520740.504999
2footwo-0.139441-1.190568
3barthree-0.9718560.340176
4footwo1.546175-0.402114
5bartwo0.0261991.313452
6fooone-0.267510-0.981974
7foothree1.0189721.100904
df.groupby('A').sum()
CD
A
bar-0.6935832.158627
foo3.008348-1.568822
df.groupby(['A','B']).sum()
CD
AB
barone0.2520740.504999
three-0.9718560.340176
two0.0261991.313452
fooone0.582641-1.077045
three1.0189721.100904
two1.406734-1.592681
<matplotlib.axes._subplots.AxesSubplot at 0x9b27b00>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值