Concat不同列上的数据帧

时间:2018-01-12 09:31:24

标签: python pandas

我有3个不同的csv文件,我正在寻找concat的值。我需要的唯一条件是第一个csv数据帧必须位于新csv的A列,B列中的第二个csv数据帧和C列中的Thirth csv数据帧。所有csv文件的行数相同。 另外,我需要将三个标题更改为['año_pasado','mes_pasado','este_mes']

import pandas as pd

df = pd.read_csv('año_pasado_subastas2.csv', sep=',')

df1 = pd.read_csv('mes_pasado_subastas2.csv', sep=',')

df2 = pd.read_csv('este_mes_subastas2.csv', sep=',')

df1
>>>
Subastas
166665859
237944547
260106086
276599496
251813654
223790056
179340698
177500866
239884764
234813107

df2
>>>
Subastas
212003586
161813617
172179313
209185016
203804433
198207783
179410798
156375658
130228140
124964988

df3
>>>
Subastas
142552750
227514418
222635042
216263925
196209965
140984000
139712089
215588302
229478041
222211457

我需要的输出是:

año_pasado,mes_pasado,este_mes
166665859,124964988,142552750
237944547,161813617,227514418
260106086,172179313,222635042
276599496,209185016,216263925
251813654,203804433,196209965
223790056,198207783,140984000
179340698,179410798,139712089
177500866,156375658,215588302
239884764,130228140,229478041
234813107,124964988,222211457

1 个答案:

答案 0 :(得分:1)

我认为如果只有一个列数据或选择列,而新列名称使用参数Series,则需要squeeze=True创建的keys df = pd.read_csv('año_pasado_subastas2.csv', squeeze=True) df1 = pd.read_csv('mes_pasado_subastas2.csv', squeeze=True) df2 = pd.read_csv('este_mes_subastas2.csv', squeeze=True) cols = ['año_pasado','mes_pasado','este_mes'] df = pd.concat([df, df1, df2], keys = cols, axis=1)

df = pd.read_csv('año_pasado_subastas2.csv')
df1 = pd.read_csv('mes_pasado_subastas2.csv')
df2 = pd.read_csv('este_mes_subastas2.csv')

cols = ['año_pasado','mes_pasado','este_mes']
df = pd.concat([df['Subastas'], df1['Subastas'], df2['Subastas']], keys = cols, axis=1)

print (df)
   año_pasado  mes_pasado   este_mes
0   166665859   212003586  142552750
1   237944547   161813617  227514418
2   260106086   172179313  222635042
3   276599496   209185016  216263925
4   251813654   203804433  196209965
5   223790056   198207783  140984000
6   179340698   179410798  139712089
7   177500866   156375658  215588302
8   239884764   130228140  229478041
9   234813107   124964988  222211457

或者:

  <script src="http://alertifyjs.com/build/alertify.js"></script>
<link rel="stylesheet" href="http://alertifyjs.com/build/css/alertify.css" />
<link rel="stylesheet" href="http://alertifyjs.com/build/css/themes/default.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<div style="width : 75%;margin : auto;">
<canvas id="Canvas" width="954" height="267"></canvas>


<button class="play">Play</button>
<button class="pause">Pause</button>

</div>

<script>

var canvas = document.getElementById('Canvas');
var context = canvas.getContext("2d");

// Map sprite
var mapSprite = new Image();
mapSprite.src = "v1.png";
 var array_x = [351,315,353,352,318,485,351,748,568,645,373,350,572,374,650,240,240,320,707,235,402,539,256,410,433,405,353,745,404,320,514,322,692,474,293,418,418,690,237,513,378,478,433,405,285,429,458,622,568,536,564,389,641,534,567,454,575,482,459,539,431,327,464,374,348,520,279,378,509,516,232,744,351,710,481,540,429,642,537,459,513,689,324,463,375,747,431,265,484,591,458,460,485,407,720,483,376,520,691,376,232,413,536,430,653,714,484,715,629,573,263,408,436,300,544,407,432,568,536,462,485,346,610,524];
 var array_y = [18,18,181,228,60,232,34,32,26,37,65,62,228,180,91,83,83,81,88,62,35,65,52,155,23,90,94,95,230,232,15,105,37,68,47,186,186,93,14,65,22,10,230,68,18,91,226,22,150,227,89,43,76,92,62,12,187,96,78,33,35,180,151,230,151,154,95,96,31,233,39,77,79,20,30,165,65,51,18,62,96,19,150,33,79,19,35,23,210,20,180,95,184,19,76,64,36,186,73,151,26,192,153,156,24,36,165,97,85,78,77,101,184,22,177,212,210,166,79,210,150,168,45,18];

 var isPaused = false;


var firstLoad = function () {
    context.font = "15px Georgia";
    context.textAlign = "center";
}

firstLoad();

var main = function () {
    draw();
};

var draw = function () {
    // Clear Canvas
    context.fillStyle = "#000";
    context.fillRect(0, 0, canvas.width, canvas.height);

    // Draw map
    context.drawImage(mapSprite, 0, 0, 954, 267);

    //draw red cross
    cross = new Image();
    cross.src = "cross.png";    
    // draw blue cross
    cross1 = new Image();
    cross1.src = "cross-blue.png";  



    var t = window.setInterval(function() {   if(!isPaused) {       event_coor();     } }, 5000);
    i=0;

    function event_coor() {
        if (i < array_x.length) {
        context.drawImage(cross, array_x[i], array_y[i], 10, 10);
        i=i+1;
    }   
    }


}

mapSprite.addEventListener('load', main);

//with jquery
$('.pause').on('click', function(e) {
  e.preventDefault();
  isPaused = true;
});

$('.play').on('click', function(e) {
  e.preventDefault();
  isPaused = false;
});
</script>