pandas read_csv根据其中一个csv列中的值创建列

时间:2016-10-09 23:33:06

标签: python pandas

我的csv数据看起来像这样:

$(document).ready(function(){
    // scroll function
 var mouseEvent = "DOMMouseScroll mousewheel wheel";
    if( $('body').hasClass('mozilla') ){
        mouseEvent = "DOMMouseScroll";
    }
    var isDoingStuff = false;
    $(window).bind(mouseEvent, function(event){
        console.log(event.originalEvent.deltaY);
        $('body').find('.scroll-sec').show();
        var windowWidth = $(window).width();
        if( windowWidth > 767){
            var headerHeight = parseInt( $('#cc-navbar').outerHeight() ) || 0;
            if( $('body').hasClass('onepage-scroll') ){
                event.preventDefault();
                var currentdiv = $('body').find('.scroll-sec.active');
                var current_id = $('body').find('.scroll-sec.active').attr('id');
                var id_num = parseInt(current_id.split(/[-]+/).pop());
                console.log(event.originalEvent.wheelDelta);
                 if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
                    //scrolled up
                    if( $('.site-footer').hasClass('at-bottom') ){
                        if(isDoingStuff) { return; }
                        isDoingStuff = true;
                        currentdiv.removeClass('active');
                        // currentdiv.prev('.scroll-sec').addClass('active');
                        $('body').find('.site-footer').removeClass('at-bottom');
                        $('body').find('.slier-menu-right a').removeClass('active');
                        id_num -= 1;
                        $('body').find('.slier-menu-right').find('a[href="#section-'+id_num+'"]').addClass('active');
                        currentdiv.css("transform", "translateY(100%)");
                        $('body').find('.site-footer').css("transform", "translateY(100%)");
                        $('body').find('#section-'+id_num).css("transform", "translateY(0)");
                        $('body').find('#section-'+id_num).addClass('active');
                        setTimeout(function() {isDoingStuff = false;}, 150);
                    }else if (!$('#section-0').hasClass("active")){
                        if(isDoingStuff) { return; }
                        isDoingStuff = true;
                        // console.log('scrolled up');
                        currentdiv.removeClass('active');
                        $('body').find('.site-footer').removeClass('at-bottom');
                        currentdiv.css("transform", "translateY(100%)");
                        $('body').find('.site-footer').css("transform", "translateY(100%)");
                        currentdiv.prevAll('.scroll-sec').css("transform", "translateY(-100%)");
                        $('body').find('.slier-menu-right a').removeClass('active');
                        id_num -= 1;
                        $('body').find('.slier-menu-right').find('a[href="#section-'+id_num+'"]').addClass('active');
                        $('body').find('#section-'+id_num).addClass('active');
                        $('body').find('#section-'+id_num).css("transform", "translateY(0)");
                        setTimeout(function() {isDoingStuff = false;}, 150);
                        $('.scroll-sec').find('.cc-scroll-down').remove();
                        $('.scroll-sec.active').append('<a class="cc-scroll-down" href="#"><span></span></a>');
                        if( id_num == 0 ){
                            if( $('nav.navbar-fixed-top').hasClass('shrink') ){
                                $('nav.navbar-fixed-top').removeClass('shrink');
                            }
                        }
                    }
                }else{
                    //scrolled down
                    if ($('#section-0').hasClass("active")){
                        if( !$('nav.navbar-fixed-top').hasClass('shrink') ){
                            $('nav.navbar-fixed-top').addClass('shrink');
                        }
                    }
                    if (!$('.prefooter-section').hasClass("active")){
                        if(isDoingStuff) { return; }
                        isDoingStuff = true;
                        // console.log('scrolled down');
                        currentdiv.removeClass('active');
                        id_num += 1;
                        $('body').find('#section-'+id_num).addClass('active');
                        if( !$('.prefooter-section').hasClass('active') ){
                            currentdiv.css("transform", "translateY(-100%)");
                            currentdiv.nextAll('.scroll-sec').css("transform", "translateY(100%)");
                            $('body').find('.slier-menu-right a').removeClass('active');
                            $('body').find('.slier-menu-right').find('a[href="#section-'+id_num+'"]').addClass('active');
                            $('body').find('#section-'+id_num).css("transform", "translateY(0)");
                            $('.scroll-sec').find('.cc-scroll-down').remove();
                            $('.scroll-sec.active').append('<a class="cc-scroll-down" href="#"><span></span></a>');
                        }else{
                            $('body').find('.prefooter-section').css('height', 'auto');
                            var prefooterHeight = parseInt( $('body').find('.prefooter-section').outerHeight() ) || 0;
                            var footerHeight = parseInt( $('.site-footer').outerHeight() ) || 0;
                            var totalHeight = prefooterHeight+footerHeight;
                            $('body').find('.slier-menu-right a').removeClass('active');
                            $('body').find('.slier-menu-right').find('a[href="#section-'+id_num+'"]').addClass('active');
                            currentdiv.css("transform", "translateY(-"+totalHeight+"px)");
                            $('.prefooter-section').css("transform", "translateY(-"+footerHeight+"px)");
                            $('body').find('.site-footer').css("transform", "translateY(0)");
                            $('body').find('.site-footer').addClass('at-bottom');
                        }
                        setTimeout(function() {isDoingStuff = false;}, 150);
                        // if( !$('.prefooter-section').hasClass('active') ){}
                    }
                }
            }
        }else{
            console.log('scrolled just');
        }
      });
      });

我想导入到DataFrame中,最左边的值是索引,中间是列标识符,最右边是该索引处列的值。

以前我将它们分开并手动导入,然后加入一个帧。我想知道是否有更快的方法可以一步到位。

1 个答案:

答案 0 :(得分:3)

使用pd.read_csv,然后pivot将数据读入数据框:

import pandas as pd
df = pd.read_csv('data', header=None, names=['index','columns','value'])
df = df.pivot('index', 'columns', 'value')
print(df)

产量

columns        SMState  actuatorLowerFrontDuty  ambientTemperature  \
index                                                                
1471361094509      NaN                     NaN                 NaN   
1471361094512      NaN                     0.0                 NaN   
1471361094515     14.0                     NaN                 NaN   
1471361094516      NaN                     NaN                 NaN   
1471361094519      NaN                     NaN                23.5   

columns        doorCtrlStatus  doorLowerPosition  doorUpperPosition  \
index                                                                 
1471361094509             NaN         -73.334887           -3.29595   
1471361094512             NaN                NaN                NaN   
1471361094515             5.0                NaN                NaN   
1471361094516             NaN                NaN                NaN   
1471361094519             NaN                NaN                NaN   

columns        lateralAccel  longitudinalAccel  sectionLowerCurrFiltered  
index                                                                     
1471361094509           NaN                NaN                       NaN  
1471361094512           NaN                NaN                      -0.2  
1471361094515           NaN                NaN                       NaN  
1471361094516         25.55              25.55                       NaN  
1471361094519           NaN                NaN                       NaN