我有两个具有相同时间分辨率的数据框。从第一个数据帧开始(我的情况是:df_data1),我只想拥有所有值['A'],其中['B'] <90。与我的第一个数据帧具有相同的时间戳记(timeindex)
df_data1 = pd.io.parsers.read_csv(station_path, skiprows=0, index_col=0, na_values=[-999], names= names_header , sep=';', header=None , squeeze=True)
date A B
16.08.2013 03:00 -1 97
16.08.2013 03:15 -1 95
16.08.2013 03:30 0 92
16.08.2013 03:45 4 90
16.08.2013 04:00 18 88
16.08.2013 04:15 42 86
16.08.2013 04:30 73 83
16.08.2013 04:45 110 81
16.08.2013 05:00 151 78
现在,我想拥有df_data ['B'] <90的所有df_data ['A']。所以我做:
df_data = df_data[(df_data['B'] < 90)]
第二个数据框如下所示:
df_data2 = pd.io.parsers.read_csv(station_path, skiprows=1, sep=";", index_col=False, header=None)
date w x y z
16.08.2013 03:00 0 0 0 0
16.08.2013 03:15 0 0 0 0
16.08.2013 03:30 0 0 0 0
16.08.2013 03:45 0 0 0 0
16.08.2013 04:00 0 0 0 0
16.08.2013 04:15 0 0 0 0
16.08.2013 04:30 47 47 48 0
16.08.2013 04:45 77 78 79 88
16.08.2013 05:00 111 112 113 125
有没有人想解决这个问题?我需要相同形状的数据框,原因是我还要计算np.corrcoef
等等。
好了,您的第一部分已经完成:
df_data = df_data[(df_data['B'] < 90)]
然后,您可以使用 df_data['A']
如果两个df中的索引值都相同,那么这应该起作用:
In [40]:
df1.loc[df_data.index]
Out[40]:
w x y z
date
2013-08-16 04:00:00 0 0 0 0
2013-08-16 04:15:00 0 0 0 0
2013-08-16 04:30:00 47 47 48 0
2013-08-16 04:45:00 77 78 79 88
2013-08-16 05:00:00 111 112 125 NaN
编辑
不清楚您为什么得到一个,KeyError
但您也可以使用以下内容:
df_data2[df_data2.index.isin(df_data1.index)]
这将处理第二个df中不存在的所有索引值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句