从 Pandas 的数据框中打印样本列集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45577630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print sample set of columns from dataframe in Pandas?
提问by daniellemarie
How do you print (in the terminal) a subset of columns from a pandas dataframe?
如何打印(在终端中)来自 Pandas 数据帧的列子集?
I don't want to remove any columns from the dataframe; I just want to see a few columns in the terminal to get an idea of how the data is pulling through.
我不想从数据框中删除任何列;我只想在终端中看到几列,以了解数据是如何通过的。
Right now, I have print(df2.head(10))
which prints the first 10 rows of the dataframe, but how to I choose a few columns to print? Can you choose columns by their indexed number and/or name?
现在,我可以 print(df2.head(10))
打印数据框的前 10 行,但是如何选择要打印的几列?您可以按索引号和/或名称选择列吗?
回答by RagingRoosevelt
print(df2[['col1', 'col2', 'col3']].head(10))
will select the top 10 rows from columns 'col1'
, 'col2'
, and 'col3'
from the dataframe without modifying the dataframe.
print(df2[['col1', 'col2', 'col3']].head(10))
将选择的前10行从列'col1'
,'col2'
以及'col3'
从数据帧,而无需修改数据帧。