用数组切片pandas dataframe列吗?

时间:2018-07-11 17:43:55

标签: python pandas

此问题涉及previous post

要在其中插入数据框和值的数组的地方:

df_new = pd.DataFrame(np.random.randn(5,7), columns=[402.3, 407.2, 412.3, 415.8, 419.9, 423.5, 428.3])
wl     = np.array([400.0, 408.2, 412.5, 417.2, 420.5, 423.3, 425.0])

我要问的另一个问题是如何切片新的数据框:

df_int = df_new.reindex(columns=df_new.columns.union(wl)).interpolate(axis=1, limit_direction='both')

因此它只包含数组 wl 中的列吗?

请注意,我正在使用的实际数据集包含480列,因此我需要自动完成一些事情,而不仅仅是分配每列的单独值。

我在Stack Overflow中没有找到这种切片的任何示例,但也许我丢失了一些东西

1 个答案:

答案 0 :(得分:1)

IIUC,您可以像这样简单地进行列过滤。

df_int[wl]

输出:

      400.0     408.2     412.5     417.2     420.5     423.3     425.0
0  0.293797  0.383745  0.424941  0.707308  0.793880 -0.233975  0.175342
1  1.306332 -0.872758 -0.301987 -0.683450 -0.534648  0.001957  0.940651
2 -0.477284 -0.076156 -0.268190  0.370769  0.434909 -0.235272  0.285097
3 -1.317292 -0.588243 -0.036146  1.169727  0.665479  0.831551  0.839762
4 -0.075600  0.166476  0.318865  0.128501 -1.167822 -1.533821 -0.795002