根据某些逻辑将数据帧列表转换为单个数据帧

时间:2018-01-29 05:58:25

标签: python pandas

我有一个数据框列表,如下所示:

async run(message, args) {
    if (args == 'mock') {
      console.log(message.author);
      message.send(Bloodmorphed,
        '1. *No bad mana mocks (funki manas)* \n' +
        '2. Minimum 500k smite must be used at all times \n' +
        '3. No causing the game to lag using skills on attack/hit/struck \n' +
        '4. Must use delerium on attack/attack or attack/hit \n' +
        '5. No use of stash is allowed \n' +
        '6. No 2nd character to view is allowed \n' +
        '7. Matches should only have the two duelist and the host in the game \n' +
        '8. No stopping your attack once you start unless the opponent and host agree \n' +
        '9. 10 minute limit \n' +
        '10. Dueling area should be cleared by the host prior to the duel \n' +
        '11. Must use Nv Valkyrie or Dopplezon \n' +
        '12. Duels last until someone dies \n' +
        '13. Any death after joining the game will count as a loss \n' +
        '14. Each player will have a chance to be 2nd joiner and 3rd joiner. Higher ranked player will be 2nd joiner first. If both are un-ranked, the challenged will be 2nd joiner first \n' +
        '15. Duels must be in a neutral game \n' +
        '16. No mercs / summoned units allowed \n');
    } else if (args == 'legit') {
      message.send('Legit rules test');
    } else {
      message.reply('Error: The command you have entered is correct. Use !help for help on commands.');
    }
  }
}

我的最终输出应该是单个数据帧,但有一些逻辑。 比方说,我的列表中有3个数据帧,我的最终结果应该是

ListofDataframes[0]
Out[26]: 
                     Column1
LastNotify                                                          
2016-11-28 00:37:07     1
ListofDataframes[1]
Out[27]: 
                     Column2
LastNotify                                                                                                     
2016-11-28 04:25:44     1                                         
ListofDataframes[2]
Out[28]: 
                     Column3
LastNotify                                                                                                       
2016-12-02 11:32:49     1
2016-12-02 11:34:19     0

其中LastNotify为1-4,因为我们在数据帧列表中总共有4个LastNotify值,而Column1在第一个条目中为1,Column2在第二个条目中为1,依此类推....

另一个先决条件是,在创建最终数据帧时,它还应该检查现有列名,如果Column1已经存在于最终数据帧中,则不应该创建具有相同名称的另一列。 / p>

1 个答案:

答案 0 :(得分:0)

我认为您需要concat,将NaN替换为fillna并转换为int,然后sort_index并删除DatetimeIndex reset_index。上次按insert添加新列:

df1 = pd.DataFrame({ 'Column1': [1]}, index=pd.to_datetime(['2016-11-28 00:37:07']))
df2 = pd.DataFrame({'Column2': [1]}, index=pd.to_datetime(['2016-11-28 04:25:44']))
df3 = pd.DataFrame({'Column1': [1,0]}, 
                    index=pd.to_datetime(['2016-12-02 11:32:49', '2016-12-02 11:34:19']))

ListofDataframes = [df1, df2, df3]
df = pd.concat(ListofDataframes).fillna(0).astype(int).sort_index().reset_index(drop=True)
df.insert(0, 'LastNotify', range(1, len(df) + 1))
print (df)
   LastNotify  Column1  Column2  Column3
0           1        1        0        0
1           2        0        1        0
2           3        0        0        1
3           4        0        0        0