Pandas DataFrame:如何水平打印单行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40622796/
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
Pandas DataFrame: How to print single row horizontally?
提问by gmsi
Single row of a DataFrame prints value side by side, i.e. column_name then columne_value in one line and next line contains next column_name and columne_value. For example, below code
DataFrame 的单行并排打印值,即一行中的 column_name 然后 columne_value 和下一行包含下一个 column_name 和 columne_value。例如,下面的代码
import pandas as pd
df = pd.DataFrame([[100,200,300],[400,500,600]])
for index, row in df.iterrows():
# other operations goes here....
print row
Output for first row comes as
第一行的输出为
0 100
1 200
2 300
Name: 0, dtype: int64
Is there a way to have each row printed horizontally and ignore the datatype, Name? Example for the first row:
有没有办法让每一行水平打印而忽略数据类型名称?第一行示例:
0 1 2
100 200 300
采纳答案by piRSquared
use the to_frame
method then transpose with T
使用该to_frame
方法然后转置T
df = pd.DataFrame([[100,200,300],[400,500,600]])
for index, row in df.iterrows():
print(row.to_frame().T)
0 1 2
0 100 200 300
0 1 2
1 400 500 600
note:
This is similar to @JohnE's answer in that the method to_frame
is syntactic sugar around pd.DataFrame
.
注意:
这类似于@JohnE 的回答,因为该方法to_frame
是围绕pd.DataFrame
.
In fact if we follow the code
事实上,如果我们按照代码
def to_frame(self, name=None):
"""
Convert Series to DataFrame
Parameters
----------
name : object, default None
The passed name should substitute for the series name (if it has
one).
Returns
-------
data_frame : DataFrame
"""
if name is None:
df = self._constructor_expanddim(self)
else:
df = self._constructor_expanddim({name: self})
return df
Points to _constructor_expanddim
指着 _constructor_expanddim
@property
def _constructor_expanddim(self):
from pandas.core.frame import DataFrame
return DataFrame
Which you can see simply returns the callable DataFrame
您可以看到它只是返回可调用的 DataFrame
回答by Boud
Use the transpose property:
使用转置属性:
df.T
0 1 2
0 100 200 300
回答by Batman
It seems like there should be a simpler answer to this, but try turning it into another DataFrame with one row.
似乎应该对此有一个更简单的答案,但请尝试将其转换为另一个带有一行的 DataFrame。
data = {x: y for x, y in zip(df.columns, df.iloc[0])}
sf = pd.DataFrame(data, index=[0])
print(sf.to_string())
回答by JohnE
Sorta combining the two previous answers, you could do:
Sorta 结合前两个答案,你可以这样做:
for index, ser in df.iterrows():
print( pd.DataFrame(ser).T )
0 1 2
0 100 200 300
0 1 2
1 400 500 600
Basically what happens is that if you extract a row or column from a dataframe, you get a series which displays as a column. And doesn't matter if you do ser
or ser.T
, it "looks" like a column. I mean, series are one dimensional, not two, but you get the point...
基本上发生的情况是,如果您从数据框中提取一行或一列,您会得到一个显示为一列的系列。并且无论您使用ser
或ser.T
,它“看起来”都像一列。我的意思是,系列是一维的,而不是二维的,但你明白了……
So anyway, you can convert the series to a dataframe with one row. (I changed the name from "row" to "ser" to emphasize what is happening above.) The key is you have to convert to a dataframe first (which will be a column by default), then transpose it.
所以无论如何,您可以将系列转换为一行的数据框。(我将名称从“row”更改为“ser”以强调上面发生的事情。)关键是您必须先转换为数据框(默认情况下将是一列),然后对其进行转置。