pandas 在数据框中打印列名称和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39535939/
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
Printing Column Names and Values in Dataframe
提问by MattR
The end goal of this question is to plot X and Y for a graph using a dataframe.
这个问题的最终目标是使用数据框绘制图形的 X 和 Y。
I have a dataframe like so:
我有一个像这样的数据框:
Open High Low Close Volume stock symbol
Date
2000-10-19 1.37 1.42 1.24 1.35 373590000 AAPL
2000-10-20 1.36 1.46 1.35 1.39 195836200 AAPL
2000-10-23 1.39 1.49 1.39 1.46 129851400 AAPL
2000-10-24 1.48 1.49 1.34 1.35 192711400 AAPL
2000-10-25 1.36 1.37 1.30 1.32 163448600 AAPL
2000-10-26 1.34 1.42 1.25 1.32 178110800 AAPL
2000-10-27 1.35 1.37 1.28 1.33 181242600 AAPL
2000-10-30 1.37 1.42 1.34 1.38 152558000 AAPL
And I am trying to plot Date
vs. Open
. I know there is a way to simply plot, but I will be applying this concept to larger dataframes and would like to know how to do it "long-hand".
我正在尝试绘制Date
vs. Open
. 我知道有一种简单的绘图方法,但我会将这个概念应用于更大的数据框,并想知道如何“长期”进行。
What I've tried:
我试过的:
print(some_DF['Open'])
print(some_DF['Open'])
Result:
结果:
Date
2000-10-19 1.37
2000-10-20 1.36
2000-10-23 1.39
2000-10-24 1.48
2000-10-25 1.36
2000-10-26 1.34
Problem:
问题:
Date seems to be my index, but the column header 'Open' Does not appear.
日期似乎是我的索引,但列标题“打开”没有出现。
Question:
题:
How do i print the above Dataframe while having 'Open'
as my header. Then making some value x
=Date
's column and some value y
= 'Open
's values?
我如何在'Open'
作为我的标题的同时打印上述数据帧。然后制作一些 value x
=Date
的列和一些 value y
='Open
的值?
"Expected Code to work":
“预期的代码工作”:
Im thinking something like
我在想类似的事情
print([some_DF['Open'] headers = 'date','open')
x = some_DF['Date'] #So that this becomes first column of dataframe
y = some_DF['Open'] #So that this becomes second column of dataframe
回答by Asish M.
You can reset_index
on the data-frame and then print the subset dataframe consisting of the two columns
您可以reset_index
在数据框上,然后打印由两列组成的子集数据框
>>> df
a b
Date
2000-10-19 1 3
2000-10-20 2 4
2000-10-21 3 5
2000-10-22 4 6
2000-10-23 5 7
>>> print(df.reset_index()[['Date', 'a']])
Date a
0 2000-10-19 1
1 2000-10-20 2
2 2000-10-21 3
3 2000-10-22 4
4 2000-10-23 5
Like IanS mentioned, you shouldn't worry about how the output looks in pandas. Date was an index and Open a column. The difference in the print statement illustrates that distinction.
就像 IanS 提到的,你不应该担心输出在 Pandas 中的样子。日期是一个索引并打开一列。打印语句中的差异说明了这种区别。
Edit:
编辑:
The df[[list_of_column_names]]
is the same as df.loc[:, [list_of_column_names]]
. It gives a list of columns to subset the original dataframe.
该df[[list_of_column_names]]
是一样的df.loc[:, [list_of_column_names]]
。它提供了一个列列表来对原始数据框进行子集化。