我試圖過濾出一些奇怪的數據框中出現一些重複的數據:'Col1'字符串元素轉換爲時間戳,恕不另行通知。我希望Col1繼續擁有字符串元素。熊貓:str類型的列轉換爲tslib.Timestamp後使用應用函數
這裏是例子:
>>> from pandas import *
>>> import datetime as DT
>>> df = DataFrame({
'Col0': 'RR0 RR1 RR2 RR3 RR4 RR5 RR6 RR7'.split(),
'Col1' : 'A7 A1 A2 A3 A4 A5 A6 A7'.split(),
'Col2' : [
DT.datetime(2013,1,1,13,0),
DT.datetime(2013,1,1,13,5),
DT.datetime(2013,10,1,20,0),
DT.datetime(2013,10,2,10,0),
DT.datetime(2013,10,1,20,0),
DT.datetime(2013,10,2,10,0),
DT.datetime(2013,12,2,12,0),
DT.datetime(2013,12,2,14,0)
],
'Col3': [1,3,5,1,8,1,9,3],
'Col4': 'L0 L1 L0 L0 L2 L2 L3 L4'.split()})
>>> df=df[['Col0','Col1','Col2','Col3','Col4']]
>>> df
Col0 Col1 Col2 Col3 Col4
0 RR0 A7 2013-01-01 13:00:00 1 L0
1 RR1 A1 2013-01-01 13:05:00 3 L1
2 RR2 A2 2013-10-01 20:00:00 5 L0
3 RR3 A3 2013-10-02 10:00:00 1 L0
4 RR4 A4 2013-10-01 20:00:00 8 L2
5 RR5 A5 2013-10-02 10:00:00 1 L2
6 RR6 A6 2013-12-02 12:00:00 9 L3
7 RR7 A7 2013-12-02 14:00:00 3 L4
# Filter the data of Col4 by oldest time register in Col2
>>> df2=df.groupby('Col4',group_keys=False,as_index=False).apply(lambda x: x.ix[x.Col2.idxmin()])
# df was filtered but Col1 was transformed to Timespan
>>> df2
Col0 Col1 Col2 Col3 Col4
0 RR0 2015-04-07 2013-01-01 13:00:00 1 L0
1 RR1 2015-04-01 2013-01-01 13:05:00 3 L1
2 RR4 2015-04-04 2013-10-01 20:00:00 8 L2
3 RR6 2015-04-06 2013-12-02 12:00:00 9 L3
4 RR7 2015-04-07 2013-12-02 14:00:00 3 L4
問題:什麼是對這種行爲的原因是什麼?有沒有辦法避免這種情況發生?
我不是複製您的結果。您使用的是什麼python和pandas。 –
@MarkGraph:我看到使用Python2.7/Pandas版本0.15.2的問題。 – unutbu
這也發生在熊貓0.16.0 – EdChum