Python。从 data.frame 获取结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36482559/
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
Python. Get structure from a data.frame
提问by Jose
In r, with the str()
function you can see structure from an object like this:
在r 中,使用该str()
函数,您可以从这样的对象中查看结构:
> str(mari)
'data.frame': 25834 obs. of 6 variables:
$ Xcoor: num 0.0457 0.0469 0.0481 0.0495 0.0519 ...
$ Ycoor: num 0.107 0.107 0.107 0.108 0.108 ...
$ Zcoor: num -0.701 -0.701 -0.701 -0.703 -0.703 ...
$ RC : int 120 124 124 125 124 122 120 120 120 120 ...
$ GC : int 121 117 117 117 118 119 120 120 120 120 ...
$ BC : int 127 135 144 135 126 127 125 125 124 137 ...
Is there a similar function like this one?
有没有类似的功能?
采纳答案by MaxNoe
If you are looking for an equivalent of R
s data.frame
, you will want to look into pandas
.
如果您正在寻找R
s的等效项data.frame
,您将需要查看pandas
.
The pandas.DataFrame
might be what you are looking for.
这pandas.DataFrame
可能是您正在寻找的。
The get an idea of what is in a DataFrame
you could use the .describe
or .head
methods.
了解DataFrame
您可以使用.describe
或.head
方法的内容。
import pandas as pd
data = pd.DataFrame({
'a': [1, 2, 3, 4, 5],
'b': [1, 2, 3, 4, 5]
})
print(data.head())
print(data.describe())
print(data.columns)
Or, which might be a little to verbose, just:
或者,这可能有点冗长,只是:
print(data)
回答by Alex B
I realize this is an old question, but wanted to provide clarification for anyone else that comes across this question in the future like I did.
我意识到这是一个老问题,但想为将来像我一样遇到这个问题的其他人提供澄清。
As MaxNoe said, pandas
is what is needed and the pandas.DataFrame.info
method is the equivalent to the str()
function in R.
正如 MaxNoe 所说,pandas
这就是所需要的,该pandas.DataFrame.info
方法相当于str()
R 中的函数。
Using the same example as MaxNoe:
使用与 MaxNoe 相同的示例:
>>> import pandas as pd
>>> data = pd.DataFrame({
'a': [1, 2, 3, 4, 5],
'b': [1, 2, 3, 4, 5]
})
>>> data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 2 columns):
a 5 non-null int64
b 5 non-null int64
dtypes: int64(2)
memory usage: 160.0 bytes
The documentation can be found here https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.info.html.
文档可以在这里找到https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.info.html。
回答by vasanth komati
The functions below may help you find the data types of a DF.
下面的函数可以帮助您找到 DF 的数据类型。
DF.info
DF.dtypes
OP:
ltv float64
branch_id int64
supplier_id int64
manufacturer_id int64
Current_pincode_ID int64
Date.of.Birth object