2017-05-14 4 views
0

Ich habe dieses DataFrame, aus einer Gruppe von gleichen Datenrahmen, aber sie haben den gleichen Spaltennamen wie total_inflow, aber ich muss diesen Namen ändern, um den anderen Ursprung jeder anderen Spalte als total_inflow_t1, total_inflow_t2 anzuzeigen ...Ändern Sie den gleichen Text in Indexspalten

so habe ich dies:

In [227]: 
all = DataFrame([node_t1["total_inflow"], node_t2["total_inflow"], node_t3["total_inflow"], node_t4["total_inflow"], node_t5["total_inflow"]]).T 



Out[227]: 
    total_inflow total_inflow total_inflow total_inflow total_inflow 
time      
01/01/01 00:01:00 0.0085 0.0040 0.0002 0.0001 0.0001 
01/01/01 00:02:00 0.2556 0.1669 0.0590 0.0012 0.0001 
01/01/01 00:03:00 0.9935 0.7699 0.3792 0.0283 0.0002 
01/01/01 00:04:00 1.3873 1.2879 0.8767 0.1614 0.0011 

so das ich brauche zu bekommen:

Out[227]: 
    total_inflow_t1  total_inflow_t2  total_inflow_t3  total_inflow_t4  total_inflow_t5 
time      
01/01/01 00:01:00 0.0085 0.0040 0.0002 0.0001 0.0001 
01/01/01 00:02:00 0.2556 0.1669 0.0590 0.0012 0.0001 
01/01/01 00:03:00 0.9935 0.7699 0.3792 0.0283 0.0002 
01/01/01 00:04:00 1.3873 1.2879 0.8767 0.1614 0.0011 
+0

Wenn eine dieser Antworten Ihr Problem gelöst hat, akzeptieren Sie es, indem Sie auf das Häkchen auf der linken Seite klicken. –

Antwort

0

nach Erstellen Sie Ihren Datenrahmen all, aktualisieren Sie die Spaltennamen mit:

all.columns = ['total_inflow_t1', 'total_inflow_t2', 'total_inflow_t3', 
       'total_inflow_t4', 'total_inflow_t5'] 
0

Einen anderen Weg, dies zu tun ist keys Argument in pd.concat zu verwenden:

all = pd.concat([node_t1["total_inflow"], node_t2["total_inflow"], 
      node_t3["total_inflow"], node_t4["total_inflow"], 
      node_t5["total_inflow"]], 
      axis=1, 
      keys=['total_inflow_t1', 'total_inflow_t2', 
        'total_inflow_t3','total_inflow_t4', 
        'total_inflow_t5']).T 
1

Dies ist eine generische Lösung. Es fügt allen Spalten ein Suffix hinzu.

df.columns = ['{}_t{}'.format(k,i+1) for i,k in enumerate(df.columns)] 
Verwandte Themen