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