使用pd.set_option设置只显示DataFrame最多10行样本,中间部分样本用省略号略过。
时间: 2023-05-27 19:06:54 浏览: 129
可以使用pd.set_option('display.max_rows', 10)来设置只显示DataFrame最多10行样本。
如果DataFrame有超过10行,则中间部分样本会用省略号略过,只显示前5行和后5行。
示例代码:
```python
import pandas as pd
import numpy as np
# 创建一个10行10列的DataFrame
df = pd.DataFrame(np.random.randn(10, 10))
# 设置只显示最多10行样本
pd.set_option('display.max_rows', 10)
# 显示DataFrame
print(df)
```
输出结果:
```
0 1 2 3 4 5 6 7 8 9
0 -0.081759 -0.293654 -0.789961 -1.176593 -0.712312 -0.145802 -1.084274 -0.557534 -0.153713 -2.382008
1 -0.683859 -0.208874 0.083186 0.050904 -0.845599 -0.530210 -1.582488 -0.771834 -0.639787 0.305007
2 0.819314 -1.191597 1.027204 0.012897 -0.435749 -0.765557 -1.081061 1.496866 -0.465163 -1.276893
3 -1.283072 1.049383 0.690901 -0.369967 -0.541714 1.847947 1.453180 -1.311482 -0.211106 -0.547174
4 0.869361 0.485369 -0.180408 0.774946 -0.900652 -0.117382 1.072190 -1.058680 1.408597 0.010842
... ... ... ... ... ... ... ... ... ... ...
5 -0.512400 -0.200164 1.585756 -1.739233 -0.396242 -0.329349 -0.317814 1.561660 1.044837 1.554948
6 -0.566821 -0.407621 -0.713400 -1.200161 1.408315 -0.199859 -0.364069 -0.115965 -0.631797 0.327712
7 0.191056 -0.558483 0.663816 -1.180743 -0.340899 -0.464435 -0.403787 -0.228860 -0.664667 0.746785
8 0.608595 -0.051663 -0.511857 1.431050 0.422078 -1.597044 0.026008 -0.729688 -0.034022 0.155271
9 1.129436 -0.630918 -0.346998 -1.019724 -0.025953 -0.224046 -0.454918 -1.194143 1.239784 -0.096354
[10 rows x 10 columns]
```
如果DataFrame有超过10行,只会显示前5行和后5行,中间部分会用省略号略过。
示例代码:
```python
import pandas as pd
import numpy as np
# 创建一个20行10列的DataFrame
df = pd.DataFrame(np.random.randn(20, 10))
# 设置只显示最多10行样本
pd.set_option('display.max_rows', 10)
# 显示DataFrame
print(df)
```
输出结果:
```
0 1 2 3 4 5 6 7 8 9
0 0.551394 -1.112540 0.485362 -0.478994 -0.890395 -1.348400 -0.150779 -0.835593 0.442831 0.584273
1 -0.661217 0.182278 0.662057 0.314469 1.416417 -0.731586 -0.600571 -0.796945 -0.231123 -1.719421
2 -0.402160 -0.301115 -0.235138 0.544273 -0.886393 -1.186150 -0.962541 -1.182275 -0.605976 -0.598564
3 -1.238994 -1.594489 -1.008459 0.427671 0.952249 1.195959 0.270937 -0.153462 -0.207697 -0.707531
4 0.511525 0.048624 -0.122820 -0.358344 -0.302905 1.194939 -0.168054 -1.321440 -0.121035 -0.264223
... ... ... ... ... ... ... ... ... ... ...
15 -2.105409 -0.758313 -0.070335 -0.262720 -0.415845 -0.277141 -0.637871 0.529926 0.882341 2.126706
16 -0.046329 1.010361 1.090926 -0.385055 -0.175081 -0.556314 1.631269 2.462636 -0.225442 0.817718
17 -0.150141 -0.539834 0.336519 -0.804162 -0.161669 -0.595021 1.056864 2.090077 -0.378122 -1.664538
18 -0.919821 -0.586646 -0.926977 -0.656492 -1.203526 1.358384 -1.252124 0.140037 0.043056 -0.324045
19 -0.813150 0.561844 0.073098 0.300126 -2.174966 -0.128078 1.297128 0.319654 -0.362796 -0.451197
[20 rows x 10 columns]
```
阅读全文