熊猫数据框-追加数据框(多列/行+多列,单行)

时间:2020-07-23 11:24:56

标签: python python-3.x pandas dataframe

数据框中的更多冒险活动:)

因此,我几乎掌握了所有基础知识,但是,这一点很困扰我。我有两个数据框(下图)。第一个(techIndicator)具有大量的列和行,并且都正确填充。第二个数据框(社交)具有多列,但只有一行。

我需要添加列(按照SS进行工作),但是我想一直复制社交数据框的行以“填充nan”。

这是我用来将所有数据框连接成一个单独的代码(社交工作除外的所有工作):

techIndicator = pd.concat([inter_day, macd, rsi, ema, vwap, adx, dmi, social], axis = 1)
techIndicator.sort_index(ascending=False, inplace=True)
techIndicator.dropna()
techIndicator.reset_index(drop=True)

根据下面的SS,前三行应如下所示:

datetime1 | 1 | 2 | 3 | 4 | ......| 9 | 8| 7 | 6
datetime2 | 2 | 1 | 4 | 3 | ......| 9 | 8| 7 | 6
datetime3 | 3 | 4 | 1 | 2 | ......| 9 | 8| 7 | 6

相反,上面的连接添加了列,但删除了值(我已经检查了数据类型,它们都是float64的)

请帮助=)我的google-fu不适用于此>。<< / p>

在下面的亚历克斯的帮助下,我能够解决我遇到的许多问题!

    dfTemp = pd.concat([inter_day, macd, rsi, ema, vwap, adx, dmi], axis = 1)
    dfTemp.sort_index(ascending=False, inplace=True)
    dfTemp.dropna()
    dfTemp.reset_index(inplace = True)
    

    long_social = social
    for a in range(dfTemp.shape[0] - 1):
        long_social=pd.concat([long_social, social])
    long_social.reset_index(inplace = True)
    long_social.drop(columns = ['index'], inplace = True)
    
    techIndicator = pd.concat([dfTemp, long_social], axis = 1)
    techIndicator.rename(columns={'date': 'Date',
                                  '1. open': 'Open',
                                  '2. high': 'High',
                                  '3. low': 'Low',
                                  '4. close': 'Close',
                                  '5. volume': 'Volume',
                                  'DX': 'DMI'}, inplace=True)

    techIndicator.dropna(inplace=True)
    techIndicator.reset_index(drop=True, inplace=True)
    techIndicator.set_index('Date', inplace=True)
    techIndicator.sort_index(ascending=False, inplace=True)

Main dataframe: techIndicator

Dataframe to append

1 个答案:

答案 0 :(得分:1)

因此,我为您提供了一个解决方案,该解决方案不使用concat将列添加到您的主要数据集中,但是可以完成工作。

它的流程是因为两个数据帧的大小不相等,因此我们将它们设置为偶数大小,并通过命名循环进行循环以添加列。

<html>
<body>

Text without typing effect

<p id="heading">Text with typing effect</p>
</body>
</html>