gpt4 book ai didi

python - 用 Pandas 随机生成一个数据集

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:35 25 4
gpt4 key购买 nike

我试图从 500 行的数据集中随机提取 150 行。所以我会随机做。

我的数据

 object,color,label
puppy,white,animal
mango,orange,fruit
pants,black,clothing

我的脚本

 import pandas
import pandas as pd

df = pd.read_csv ('data.csv', sep = ',')
data_random = df.sample (150)

print (data_random)

但是结果很奇怪,我没有完整的行...

     object ... label
277 grape ... fruit
116 bear ... animal
495 ... ... ...

是否可以有完整的行?

最佳答案

可以查看docs对于 Pandas 0.23.0:

Previously, the default value for the maximum number of columns was pd.options.display.max_columns=20. This meant that relatively wide data frames would not fit within the terminal width, and pandas would introduce line breaks to display these 20 columns.

If Python runs in a terminal, the maximum number of columns is now determined automatically so that the printed data frame fits within the current terminal width (pd.options.display.max_columns=0) (GH17023). If Python runs as a Jupyter kernel (such as the Jupyter QtConsole or a Jupyter notebook, as well as in many IDEs), this value cannot be inferred automatically and is thus set to 20 as in previous versions. In a terminal, this results in a much nicer output.


Note that if you don’t like the new default, you can always set this option yourself. To revert to the old setting, you can run this line:

pd.options.display.max_columns = 20

关于python - 用 Pandas 随机生成一个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51999891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com