来自groupby pandas python的字符串的concat部分

时间:2014-01-13 19:13:22

标签: python pandas

在执行groupby之后,如何使用文件名连接日期的一部分 我想最终得到一个数组: '你好2014年1月2日','你好2014年1月2日'

我的代码结果令人惊讶。

import pandas as pd
from datetime import datetime
d = {   'File' : pd.Series(['hello', 'what']), 
    'Status' : pd.Series([0., 0.]), 
    'Error' : pd.Series([2., 2.]), 
    'AlertDays' : pd.Series([2., 2.]), 
    'Date' : pd.Series([datetime(2014, 1, 2), datetime(2014, 1, 2)])}
df=pd.DataFrame(d)
df['Date']=pd.to_datetime(df['Date'])
Faildf=df[df.Status==0]
Fx=Faildf.groupby('File')['Date'].max().reset_index()
Fx['concat']=Fx['File'] +' '+ str(Fx['Date'])
#FailArray=Fx['concat'].unique()

为什么会有多个日期...我以为我做了groupby和max而失去了其他日期?结果:

>>> Fx
    File                Date                                             concat
0  hello 2012-05-02 00:00:00  0   2012-05-02 00:00:00\n1   2012-05-02 00:00:...
1   what 2012-05-02 00:00:00  0   2012-05-02 00:00:00\n1   2012-05-02 00:00:...

1 个答案:

答案 0 :(得分:1)

问题在于您将pandas Series Fx['File']与pandas Series str(Fx['Date'])的字符串表示形式连接起来,您需要做的是将str强制转换函数应用于元素这样的Fx['Date']

>>> Fx['File'] + " " + Fx['Date'].apply(str)
0    hello 2014-01-02 00:00:00
1     what 2014-01-02 00:00:00