Pandas提供了把多个DataFrame合并链接成一个DataFrame的concat的方法:
In [2]: import pandas as pd In [3]: import numpy as np In [4]: data = pd.DataFrame(np.random.randn(10, 4)) In [5]: data Out[5]: 0 1 2 3 0 -0.150377 0.473775 -0.564428 1.867808 1 0.178880 -0.356651 -0.864143 -0.325870 2 0.635222 -0.502338 -0.702672 1.469388 3 -0.921042 -0.143417 0.629042 -1.312538 4 0.224232 0.414366 -0.575869 -1.002100 5 -1.155228 -0.739624 0.131099 -1.037161 6 -1.782056 -0.316029 -0.005173 1.159687 7 0.878195 0.436940 -0.048127 -1.952570 8 1.511242 -0.189323 -2.011342 0.178081 9 -0.547744 0.371512 1.231758 0.578528 In [6]: groups = [data[:3], data[3:7], data[7:]] In [7]: groups Out[7]: [ 0 1 2 3 0 -0.150377 0.473775 -0.564428 1.867808 1 0.178880 -0.356651 -0.864143 -0.325870 2 0.635222 -0.502338 -0.702672 1.469388, 0 1 2 3 3 -0.921042 -0.143417 0.629042 -1.312538 4 0.224232 0.414366 -0.575869 -1.002100 5 -1.155228 -0.739624 0.131099 -1.037161 6 -1.782056 -0.316029 -0.005173 1.159687, 0 1 2 3 7 0.878195 0.436940 -0.048127 -1.952570 8 1.511242 -0.189323 -2.011342 0.178081 9 -0.547744 0.371512 1.231758 0.578528] In [8]: pd.concat(groups) Out[8]: 0 1 2 3 0 -0.150377 0.473775 -0.564428 1.867808 1 0.178880 -0.356651 -0.864143 -0.325870 2 0.635222 -0.502338 -0.702672 1.469388 3 -0.921042 -0.143417 0.629042 -1.312538 4 0.224232 0.414366 -0.575869 -1.002100 5 -1.155228 -0.739624 0.131099 -1.037161 6 -1.782056 -0.316029 -0.005173 1.159687 7 0.878195 0.436940 -0.048127 -1.952570 8 1.511242 -0.189323 -2.011342 0.178081 9 -0.547744 0.371512 1.231758 0.578528 复制代码
Pandas支持类似sql中的join链接:
In [10]: l = pd.DataFrame({"key": ["foo", "bar"], "lv": [1, 2]}) ...: In [11]: ri = pd.DataFrame({"key": ["foo", "bar"], "rv": [4, 5]} ...: ) In [12]: l Out[12]: key lv 0 foo 1 1 bar 2 In [13]: ri Out[13]: key rv 0 foo 4 1 bar 5 In [15]: pd.merge(l, ri, on='key') Out[15]: key lv rv 0 foo 1 4 1 bar 2 5 复制代码
给DataFrame追加行:
In [16]: data = pd.DataFrame(np.random.randn(8, 4), columns=['A' ...: , 'B', 'C', 'D']) In [17]: data Out[17]: A B C D 0 -2.403072 0.523013 -1.730440 -1.050905 1 0.110529 1.797760 0.583266 -0.191529 2 0.308775 -0.904275 0.034278 -1.340783 3 0.931248 0.040340 0.540556 -0.294532 4 0.343270 0.527614 -1.213862 -0.435943 5 -0.887317 -1.292721 0.433839 0.401957 6 -0.037427 0.148965 0.818236 -0.062046 7 -0.537390 0.703600 0.470049 0.420687