如何在数据帧的开头插入列向量?

时间:2019-02-04 09:09:53

标签: python pandas numpy

我有以下两个变量:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="opcode" match="record" use="OperationID" />
<xsl:key name="combination" match="record" use="concat(COMBINATION_CODE, '|', OperationID)" />   

<xsl:template match="/root">
    <Show releaseID="5.4.4" xmlns:star="http://www.starstandard.org/STAR/5">
        <DataArea>
            <LOperations>
                <!-- for each distinct OperationID -->
                <xsl:for-each select="records/record[count(. | key('opcode', OperationID)[1]) = 1]" >          
                    <LOperationsDetail>
                        <LOperationID><xsl:value-of select="OperationID"/></LOperationID>
                        <!-- for each distinct COMBINATION_CODE in the current group -->
                        <xsl:for-each select="key('opcode', OperationID)[count(. | key('combination', concat(COMBINATION_CODE, '|', OperationID))[1]) = 1]" >
                            <Combinations>
                                <combinationCode><xsl:value-of select="COMBINATION_CODE"/></combinationCode>
                                <!-- get current sub-group -->
                                <xsl:for-each select="key('combination', concat(COMBINATION_CODE, '|', OperationID))">
                                    <VLaborAllowance  xmlns:star="http://www.starstandard.org/STAR/5" >
                                        <VIGroup>
                                            <GID><xsl:value-of select="AllGroupID" /></GID>
                                            <VID><xsl:value-of select="AllID" /></VID>
                                        </VIGroup>
                                    </VLaborAllowance>
                                </xsl:for-each>
                            </Combinations>
                        </xsl:for-each>
                    </LOperationsDetail>
                </xsl:for-each>
            </LOperations>
        </DataArea>
    </Show>
</xsl:template>

</xsl:stylesheet>

并输出:

print('Column vector type %s and shape %s' % (type(target), target[0:X_train.shape[0]].shape))
print('Data frame type %s and shape %s' % (type(X_train), X_train.shape))

我想插入Column vector type <class 'numpy.ndarray'> and shape (87145,) Data frame type <class 'pandas.core.frame.DataFrame'> and shape (87145, 11) 列向量作为该帧/矩阵的第一列...我该怎么做?

我的最终目标是能够使用target函数来计算附加到预测变量或设计矩阵上的响应变量的相关矩阵。

1 个答案:

答案 0 :(得分:1)

使用DataFrame.insert

X_train.insert(0,'target',target) 
print (X_train)

用于分配相同长度的数组,例如DataFrame

X_train.insert(0,'target',target[:X_train.shape[0]]) 
print (X_train)