1. import numpy as np
    2. import pandas as pd
    1. df = pd.DataFrame([['zs', 12], ['ls', 4]], columns = ['Name','Age'])
    2. df
    Name Age
    0 zs 12
    1 ls 4
    1. df2 = pd.DataFrame([['ww', 16], ['zl', 8]], columns = ['Name','Age'])
    2. df2
    Name Age
    0 ww 16
    1 zl 8
    1. df = df.append(df2)
    1. df['Name'][0] = 'Tom'
    2. print(df)
    1. Name Age
    2. 0 Tom 12
    3. 1 ls 4
    4. 0 Tom 16
    5. 1 zl 8
    6. E:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning:
    7. A value is trying to be set on a copy of a slice from a DataFrame
    8. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
    9. """Entry point for launching an IPython kernel.
    1. s = pd.Series(range(5))
    2. s
    1. 0 0
    2. 1 1
    3. 2 2
    4. 3 3
    5. 4 4
    6. dtype: int64
    1. s.where(s > 0,other=100)
    1. 0 100
    2. 1 1
    3. 2 2
    4. 3 3
    5. 4 4
    6. dtype: int64
    1. s.mask(s>0,other=100) #与where相反
    1. 0 0
    2. 1 100
    3. 2 100
    4. 3 100
    5. 4 100
    6. dtype: int64
    1. df = pd.DataFrame({'A': range(1, 6),
    2. 'B': range(10, 0, -2),
    3. 'C C': range(10, 5, -1)})
    4. df
    A B C C
    0 1 10 10
    1 2 8 9
    2 3 6 8
    3 4 4 7
    4 5 2 6
    1. df.query('A > B')
    A B C C
    4 5 2 6
    1. df[df.A > df.B]
    A B C C
    4 5 2 6

    算术运算

    1. df = pd.DataFrame(np.random.randn(8, 3), columns=list('ABC'))
    2. df
    A B C
    0 -0.153609 0.114352 -0.653078
    1 -0.098363 -1.239002 1.650134
    2 1.208225 -0.660673 -0.667172
    3 0.939248 -0.047296 -0.388360
    4 -0.784888 0.832674 -0.201554
    5 -1.027445 -0.273085 1.570641
    6 -1.602814 1.011551 0.273685
    7 0.643500 -0.494196 -1.586398
    1. df*5 + 2 #触发广播机制
    A B C
    0 1.231954 2.571762 -1.265392
    1 1.508184 -4.195011 10.250668
    2 8.041125 -1.303367 -1.335858
    3 6.696242 1.763522 0.058201
    4 -1.924438 6.163368 0.992228
    5 -3.137223 0.634576 9.853206
    6 -6.014069 7.057756 3.368424
    7 5.217502 -0.470982 -5.931991
    1. 1/df
    A B C
    0 -6.510027 8.744892 -1.531210
    1 -10.166411 -0.807101 0.606012
    2 0.827660 -1.513607 -1.498865
    3 1.064681 -21.143593 -2.574932
    4 -1.274068 1.200951 -4.961438
    5 -0.973289 -3.661867 0.636683
    6 -0.623903 0.988581 3.653837
    7 1.554001 -2.023487 -0.630359