我正在使用Python,并具有一个带有日期时间索引,分组变量(gvar)和值变量(x)的数据框。 我想找到两个组之间所有常见的日期时间。
我已经有了使用functools的解决方案,但是我正在寻找一种仅使用pandas功能(如果可能)的方法。
import functools
import pandas as pd
gvar = ['A', 'A', 'A', 'B', 'B', 'B']
x = [100, 200, 100, 200 , 100, 200]
ind = ['2018-01-01','2018-01-02', '2018-01-03', '2018-01-03', '2018-01-04', '2018-01-05' ]
df = pd.DataFrame(data={'gvar':gvar, 'x': x}, index=pd.to_datetime(ind))
common_time = functools.reduce(lambda x, y: pd.np.intersect1d(x, y), [df[df.gvar == x].index
for x in set(df.gvar)])
common_time
Out[39]: array(['2018-01-03T00:00:00.000000000'], dtype='datetime64[ns]')
欢迎提出所有建议。
答案 0 :(得分:1)
这应该做到:
>>> df.reset_index().loc[df['gvar'].reset_index().drop_duplicates().duplicated('index'),'index'].tolist()
返回:
[Timestamp('2018-01-03 00:00:00')]
如果需要相应的组或值:
>>>df[df.index.isin(df.reset_index().loc[df['gvar'].reset_index().drop_duplicates().duplicated('index'),'index'].tolist())]
给你
gvar x
2018-01-03 A 100
2018-01-03 B 200
答案 1 :(得分:1)
@Injectable( {
providedIn: 'root'
} )
export class TracksService {
protected request: number = 0;
protected ipc : IpcRenderer;
constructor( protected http: HttpClient ) {
if ( ( <any>window ).require ) {
try {
this.ipc = ( <any>window ).require( "electron" ).ipcRenderer;
} catch ( error ) {
throw error;
}
} else {
console.warn( "Could not load electron ipc" );
}
}
getDataTable( dataTablesParameters : any) : Observable<any[]>{
var id = this.request;
this.request += 1;
this.ipc.send( "getData" , id, dataTablesParameters);
return new Observable<any[]>(observer => {
this.ipc.once( "getData-" + id, ( event, arg ) => {
dataTablesParameters.recordsTotal= arg.recordsTotal;
dataTablesParameters.recordsFiltered=arg.recordsFiltered;
observer.next(arg.data);
});
});
}
}
df_filtered=df[df.groupby(level=0)['gvar'].transform('size').ge(2)]
print(df_filtered)
gvar x
2018-01-03 A 100
2018-01-03 B 200