pandas数据框中的第一列不是列? [英] First column in pandas dataframe is not a column?
问题描述
我有一个名为dfimp
的数据框:
I have a dataframe named dfimp
:
>>dfimp
Column1 Column2
vo_11
102 0.023002 0
301 3571.662104 0
302 1346.910261 0
...
在我的菜鸟看来,它的三栏是?但是:
In my noob opinion its three columns? But:
>>dfimp.dtypes
Column1 float64
Column2 float64
dtype: object
那么它的两栏?第一个(vo_11
)叫什么?我想将其用于合并,但是当我这样做时出现错误,提示没有名为vo_11
的列.
So its two columns? What is the first one (vo_11
) called? I want to use it for a merge but when i do i get error saying there is no column named vo_11
.
推荐答案
它称为index
,通过以下方法进行检查:
It is called index
, check it by:
print (df.index)
Int64Index([102, 301, 302], dtype='int64', name='vo_11')
还要检查文档:
pandas对象中的轴标签信息有许多用途:
The axis labeling information in pandas objects serves many purposes:
-使用已知的指标标识数据(即提供元数据),这对于分析,可视化和交互式控制台显示很重要
-启用自动和明确的数据对齐方式
-允许直观地获取和设置数据集的子集
-Identifies data (i.e. provides metadata) using known indicators, important for analysis, visualization, and interactive console display
-Enables automatic and explicit data alignment
-Allows intuitive getting and setting of subsets of the data set
如果需要通过 merge
的索引都DataFrames
:
df = pd.merge(df1, df2, left_index=True, right_index=True)
或使用 concat
:>
df = pd.concat([df1, df2], axis=1)
注意:
用于匹配相同类型的需要索引-int
或object
(显然是string
)
For matching need indexes of same types - both int
or object
(obviously string
)
示例:
df1 = pd.DataFrame({
'Column1': {302: 10, 301: 21, 102: 2},
'Column2': {302: 0, 301: 0, 102: 0}})
print (df1)
Column1 Column2
102 2 0
301 21 0
302 10 0
df2 = pd.DataFrame({
'Column1': {302: 4, 301: 5, 304: 6},
'Column2': {302: 0, 301: 0, 304: 0}})
print (df2)
Column1 Column2
301 5 0
302 4 0
304 6 0
df = pd.merge(df1, df2, left_index=True, right_index=True)
print (df)
Column1_x Column2_x Column1_y Column2_y
301 21 0 5 0
302 10 0 4 0
df = pd.merge(df1, df2, left_index=True, right_index=True, how='outer')
print (df)
Column1_x Column2_x Column1_y Column2_y
102 2.0 0.0 NaN NaN
301 21.0 0.0 5.0 0.0
302 10.0 0.0 4.0 0.0
304 NaN NaN 6.0 0.0
df = pd.concat([df1, df2], axis=1)
print (df)
Column1 Column2 Column1 Column2
102 2.0 0.0 NaN NaN
301 21.0 0.0 5.0 0.0
302 10.0 0.0 4.0 0.0
304 NaN NaN 6.0 0.0
df = pd.concat([df1, df2], axis=1, join='inner')
print (df)
Column1 Column2 Column1 Column2
301 21 0 5 0
302 10 0 4 0
这篇关于pandas数据框中的第一列不是列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!