如何加入Pandas中的列?

时间:2015-05-13 20:29:53

标签: python pandas

我有以下数据框:

      IDENT   YEAR   MONTH   DAY   HOUR   MIN    XXXX   YYYY  GPS   SNR      

0         0   2015       5    13      5     0  20.45     16          0    44   
1         0   2015       5    13      4     0  20.43     16          0    44   
2         0   2015       5    13      3     0  20.42     16          0    44   
3         0   2015       5    13      2     0  20.47     16          0    40   
4         0   2015       5    13      1     0  20.50     16          0    44   
5         0   2015       5    13      0     0  20.54     16          0    44   
6         0   2015       5    12     23     0  20.56     16          0    40   

它来自csv文件,我使用Python Pandas创建了数据框。

现在我想加入YEAR + MONTH + DAY + HOUR + MIN列来创建一个新列,例如

DATE-TIME    
2015-5-13-5-0 

我该怎么做?

2 个答案:

答案 0 :(得分:0)

date_cols = ['YEAR','MONTH','DAY','HOUR','MIN']
df[date_cols] = df[date_cols].astype(str)
df['the_date'] = df[date_cols].apply(lambda x: '-'.join(x),axis=1)

输出:

   IDENT  YEAR MONTH DAY HOUR MIN   XXXX  YYYY  GPS  SNR        the_date
0      0  2015     5  13    5   0  20.45    16    0   44   2015-5-13-5-0
1      0  2015     5  13    4   0  20.43    16    0   44   2015-5-13-4-0
2      0  2015     5  13    3   0  20.42    16    0   44   2015-5-13-3-0
3      0  2015     5  13    2   0  20.47    16    0   40   2015-5-13-2-0
4      0  2015     5  13    1   0  20.50    16    0   44   2015-5-13-1-0
5      0  2015     5  13    0   0  20.54    16    0   44   2015-5-13-0-0
6      0  2015     5  12   23   0  20.56    16    0   40  2015-5-12-23-0

答案 1 :(得分:0)

df.loc[:, 'DATE-TIME'] = df.apply(lambda x: "{0}-{1}-{2}-{3}-{4}"
                                            .format(int(x.YEAR), 
                                                    int(x.MONTH), 
                                                    int(x.DAY), 
                                                    int(x.HOUR), 
                                                    int(x.MIN)), 
                                  axis=1)

>>> df
   IDENT  YEAR  MONTH  DAY  HOUR  MIN   XXXX  YYYY  GPS  SNR       DATE-TIME
0      0  2015      5   13     5    0  20.45    16    0   44   2015-5-13-5-0
1      0  2015      5   13     4    0  20.43    16    0   44   2015-5-13-4-0
2      0  2015      5   13     3    0  20.42    16    0   44   2015-5-13-3-0
3      0  2015      5   13     2    0  20.47    16    0   40   2015-5-13-2-0
4      0  2015      5   13     1    0  20.50    16    0   44   2015-5-13-1-0
5      0  2015      5   13     0    0  20.54    16    0   44   2015-5-13-0-0
6      0  2015      5   12    23    0  20.56    16    0   40  2015-5-12-23-0