pandas 如何使用给定的对或行索引和列标签对 Dataframe 进行子集化?

qfe3c7zg  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(120)

下面给出了一个 Dataframe (df_path),其中的索引对应于要从中复制值的 Dataframe (df_from)的索引,值表示要从中复制值的 Dataframe 的列。

**df_path**
{0: {Timestamp('2017-04-05 10:18:02.095000'): 0,
  Timestamp('2017-04-05 10:35:03.740000'): 0,
  Timestamp('2017-04-05 10:57:18.364000'): 0,
  Timestamp('2017-04-05 11:10:09.142000'): 0,
  Timestamp('2017-04-07 09:41:11.167000'): 0,
  Timestamp('2017-04-07 09:47:22.457000'): 0,
  Timestamp('2017-04-07 09:51:22.037000'): 0,
  Timestamp('2017-04-07 09:54:59.803000'): 0,
  Timestamp('2017-04-07 09:58:49.512000'): 0,
  Timestamp('2017-04-07 10:05:45.506000'): 0,
  Timestamp('2017-04-07 10:06:38.567000'): 0,
  Timestamp('2017-04-24 09:32:06.261000'): 0,
  Timestamp('2017-05-10 09:36:56.943000'): 0,
  Timestamp('2017-05-29 09:31:32.211000'): 0,
  Timestamp('2017-06-19 09:33:56.391000'): 0,
  Timestamp('2017-06-19 09:36:11.743000'): 0,
  Timestamp('2017-06-28 10:06:58.320000'): 1,
  Timestamp('2017-06-28 10:12:04.859000'): 1,
  Timestamp('2017-07-10 09:58:36.082000'): 1,
  Timestamp('2017-07-11 09:43:03.421000'): 1,
  Timestamp('2017-07-12 09:27:27.504000'): 2,
  Timestamp('2017-07-12 09:31:16.304000'): 2,
  Timestamp('2017-07-12 09:32:47.592000'): 2,
  Timestamp('2017-07-12 09:33:43.216000'): 2,
  Timestamp('2017-07-26 09:19:23.656000'): 2,
  Timestamp('2017-07-26 09:32:07.647000'): 2,
  Timestamp('2017-07-26 09:34:16.047000'): 2,
  Timestamp('2017-07-26 09:36:29.241000'): 2,
  Timestamp('2017-07-26 09:41:14.152000'): 2,
  Timestamp('2017-07-26 09:45:01.198000'): 2,
  Timestamp('2017-07-26 09:49:06.674000'): 2,
  Timestamp('2017-08-07 09:17:59.231000'): 2,
  Timestamp('2017-08-07 09:25:57.865000'): 2,
  Timestamp('2017-08-07 09:31:29.751000'): 2,
  Timestamp('2017-08-07 09:35:27.062000'): 2,
  Timestamp('2017-08-15 09:23:40.111000'): 3,
  Timestamp('2017-08-16 09:17:48.032000'): 3,
  Timestamp('2017-08-16 09:20:37.396000'): 3,
  Timestamp('2017-08-16 09:26:34.631000'): 3,
  Timestamp('2017-08-16 10:01:35.525000'): 3,
  Timestamp('2017-08-16 10:06:06.222000'): 3,
  Timestamp('2017-08-16 10:38:44.717000'): 3,
  Timestamp('2017-08-17 09:17:18.951000'): 3,
  Timestamp('2017-08-17 09:21:33.846000'): 3,
  Timestamp('2017-08-17 09:28:14.337000'): 3,
  Timestamp('2017-08-17 09:30:42.855000'): 3,
  Timestamp('2017-08-17 09:31:35.894000'): 3,
  Timestamp('2017-08-17 09:33:15.819000'): 3,
  Timestamp('2017-08-17 09:35:23.751000'): 3,
  Timestamp('2017-08-17 09:38:45.211000'): 3,
  Timestamp('2017-08-17 09:41:25.251000'): 3,
  Timestamp('2017-08-17 09:45:21.319000'): 3,
  Timestamp('2017-08-17 09:47:55.097000'): 3,
  Timestamp('2017-08-17 09:50:17.234000'): 3,
  Timestamp('2017-08-17 09:51:49.333000'): 3,
  Timestamp('2017-08-18 10:13:44.958000'): 4,
  Timestamp('2017-08-18 10:19:25.371000'): 4,
  Timestamp('2017-08-18 10:25:33.984000'): 4,
  Timestamp('2017-08-18 10:31:29.450000'): 4,
  Timestamp('2017-08-18 10:42:42.320000'): 4,
  Timestamp('2017-08-18 10:53:34.495000'): 4,
  Timestamp('2017-08-29 09:38:10.660000'): 4,
  Timestamp('2017-08-29 09:42:50.701000'): 4,
  Timestamp('2017-08-29 09:45:21.301000'): 4,
  Timestamp('2017-09-04 09:34:11.032000'): 4,
  Timestamp('2017-09-07 09:34:48.306000'): 4,
  Timestamp('2017-09-07 09:45:30.120000'): 4,
  Timestamp('2017-09-11 09:16:19.693000'): 4,
  Timestamp('2017-09-11 09:19:50.156000'): 4,
  Timestamp('2017-09-11 09:30:40.390000'): 4},
 1: {Timestamp('2017-04-05 10:18:02.095000'): 1,
  Timestamp('2017-04-05 10:35:03.740000'): 1,
  Timestamp('2017-04-05 10:57:18.364000'): 1,
  Timestamp('2017-04-05 11:10:09.142000'): 1,
  Timestamp('2017-04-07 09:41:11.167000'): 1,
  Timestamp('2017-04-07 09:47:22.457000'): 1,
  Timestamp('2017-04-07 09:51:22.037000'): 1,
  Timestamp('2017-04-07 09:54:59.803000'): 1,
  Timestamp('2017-04-07 09:58:49.512000'): 1,
  Timestamp('2017-04-07 10:05:45.506000'): 1,
  Timestamp('2017-04-07 10:06:38.567000'): 1,
  Timestamp('2017-04-24 09:32:06.261000'): 1,
  Timestamp('2017-05-10 09:36:56.943000'): 5,
  Timestamp('2017-05-29 09:31:32.211000'): 5,
  Timestamp('2017-06-19 09:33:56.391000'): 5,
  Timestamp('2017-06-19 09:36:11.743000'): 5,
  Timestamp('2017-06-28 10:06:58.320000'): 5,
  Timestamp('2017-06-28 10:12:04.859000'): 5,
  Timestamp('2017-07-10 09:58:36.082000'): 5,
  Timestamp('2017-07-11 09:43:03.421000'): 5,
  Timestamp('2017-07-12 09:27:27.504000'): 6,
  Timestamp('2017-07-12 09:31:16.304000'): 6,
  Timestamp('2017-07-12 09:32:47.592000'): 6,
  Timestamp('2017-07-12 09:33:43.216000'): 6,
  Timestamp('2017-07-26 09:19:23.656000'): 6,
  Timestamp('2017-07-26 09:32:07.647000'): 6,
  Timestamp('2017-07-26 09:34:16.047000'): 6,
  Timestamp('2017-07-26 09:36:29.241000'): 6,
  Timestamp('2017-07-26 09:41:14.152000'): 6,
  Timestamp('2017-07-26 09:45:01.198000'): 6,
  Timestamp('2017-07-26 09:49:06.674000'): 6,
  Timestamp('2017-08-07 09:17:59.231000'): 6,
  Timestamp('2017-08-07 09:25:57.865000'): 6,
  Timestamp('2017-08-07 09:31:29.751000'): 6,
  Timestamp('2017-08-07 09:35:27.062000'): 6,
  Timestamp('2017-08-15 09:23:40.111000'): 7,
  Timestamp('2017-08-16 09:17:48.032000'): 7,
  Timestamp('2017-08-16 09:20:37.396000'): 7,
  Timestamp('2017-08-16 09:26:34.631000'): 7,
  Timestamp('2017-08-16 10:01:35.525000'): 7,
  Timestamp('2017-08-16 10:06:06.222000'): 7,
  Timestamp('2017-08-16 10:38:44.717000'): 7,
  Timestamp('2017-08-17 09:17:18.951000'): 7,
  Timestamp('2017-08-17 09:21:33.846000'): 7,
  Timestamp('2017-08-17 09:28:14.337000'): 7,
  Timestamp('2017-08-17 09:30:42.855000'): 7,
  Timestamp('2017-08-17 09:31:35.894000'): 7,
  Timestamp('2017-08-17 09:33:15.819000'): 7,
  Timestamp('2017-08-17 09:35:23.751000'): 7,
  Timestamp('2017-08-17 09:38:45.211000'): 7,
  Timestamp('2017-08-17 09:41:25.251000'): 7,
  Timestamp('2017-08-17 09:45:21.319000'): 7,
  Timestamp('2017-08-17 09:47:55.097000'): 7,
  Timestamp('2017-08-17 09:50:17.234000'): 7,
  Timestamp('2017-08-17 09:51:49.333000'): 7,
  Timestamp('2017-08-18 10:13:44.958000'): 8,
  Timestamp('2017-08-18 10:19:25.371000'): 8,
  Timestamp('2017-08-18 10:25:33.984000'): 8,
  Timestamp('2017-08-18 10:31:29.450000'): 8,
  Timestamp('2017-08-18 10:42:42.320000'): 8,
  Timestamp('2017-08-18 10:53:34.495000'): 8,
  Timestamp('2017-08-29 09:38:10.660000'): 8,
  Timestamp('2017-08-29 09:42:50.701000'): 8,
  Timestamp('2017-08-29 09:45:21.301000'): 8,
  Timestamp('2017-09-04 09:34:11.032000'): 8,
  Timestamp('2017-09-07 09:34:48.306000'): 8,
  Timestamp('2017-09-07 09:45:30.120000'): 8,
  Timestamp('2017-09-11 09:16:19.693000'): 8,
  Timestamp('2017-09-11 09:19:50.156000'): 8,
  Timestamp('2017-09-11 09:30:40.390000'): 8},
 2: {Timestamp('2017-04-05 10:18:02.095000'): 2,
  Timestamp('2017-04-05 10:35:03.740000'): 2,
  Timestamp('2017-04-05 10:57:18.364000'): 2,
  Timestamp('2017-04-05 11:10:09.142000'): 2,
  Timestamp('2017-04-07 09:41:11.167000'): 2,
  Timestamp('2017-04-07 09:47:22.457000'): 2,
  Timestamp('2017-04-07 09:51:22.037000'): 2,
  Timestamp('2017-04-07 09:54:59.803000'): 2,
  Timestamp('2017-04-07 09:58:49.512000'): 2,
  Timestamp('2017-04-07 10:05:45.506000'): 2,
  Timestamp('2017-04-07 10:06:38.567000'): 2,
  Timestamp('2017-04-24 09:32:06.261000'): 2,
  Timestamp('2017-05-10 09:36:56.943000'): 6,
  Timestamp('2017-05-29 09:31:32.211000'): 6,
  Timestamp('2017-06-19 09:33:56.391000'): 6,
  Timestamp('2017-06-19 09:36:11.743000'): 6,
  Timestamp('2017-06-28 10:06:58.320000'): 9,
  Timestamp('2017-06-28 10:12:04.859000'): 9,
  Timestamp('2017-07-10 09:58:36.082000'): 9,
  Timestamp('2017-07-11 09:43:03.421000'): 9,
  Timestamp('2017-07-12 09:27:27.504000'): 9,
  Timestamp('2017-07-12 09:31:16.304000'): 9,
  Timestamp('2017-07-12 09:32:47.592000'): 9,
  Timestamp('2017-07-12 09:33:43.216000'): 9,
  Timestamp('2017-07-26 09:19:23.656000'): 9,
  Timestamp('2017-07-26 09:32:07.647000'): 9,
  Timestamp('2017-07-26 09:34:16.047000'): 9,
  Timestamp('2017-07-26 09:36:29.241000'): 9,
  Timestamp('2017-07-26 09:41:14.152000'): 9,
  Timestamp('2017-07-26 09:45:01.198000'): 9,
  Timestamp('2017-07-26 09:49:06.674000'): 9,
  Timestamp('2017-08-07 09:17:59.231000'): 9,
  Timestamp('2017-08-07 09:25:57.865000'): 9,
  Timestamp('2017-08-07 09:31:29.751000'): 9,
  Timestamp('2017-08-07 09:35:27.062000'): 9,
  Timestamp('2017-08-15 09:23:40.111000'): 10,
  Timestamp('2017-08-16 09:17:48.032000'): 10,
  Timestamp('2017-08-16 09:20:37.396000'): 10,
  Timestamp('2017-08-16 09:26:34.631000'): 10,
  Timestamp('2017-08-16 10:01:35.525000'): 10,
  Timestamp('2017-08-16 10:06:06.222000'): 10,
  Timestamp('2017-08-16 10:38:44.717000'): 10,
  Timestamp('2017-08-17 09:17:18.951000'): 10,
  Timestamp('2017-08-17 09:21:33.846000'): 10,
  Timestamp('2017-08-17 09:28:14.337000'): 10,
  Timestamp('2017-08-17 09:30:42.855000'): 10,
  Timestamp('2017-08-17 09:31:35.894000'): 10,
  Timestamp('2017-08-17 09:33:15.819000'): 10,
  Timestamp('2017-08-17 09:35:23.751000'): 10,
  Timestamp('2017-08-17 09:38:45.211000'): 10,
  Timestamp('2017-08-17 09:41:25.251000'): 10,
  Timestamp('2017-08-17 09:45:21.319000'): 10,
  Timestamp('2017-08-17 09:47:55.097000'): 10,
  Timestamp('2017-08-17 09:50:17.234000'): 10,
  Timestamp('2017-08-17 09:51:49.333000'): 10,
  Timestamp('2017-08-18 10:13:44.958000'): 11,
  Timestamp('2017-08-18 10:19:25.371000'): 11,
  Timestamp('2017-08-18 10:25:33.984000'): 11,
  Timestamp('2017-08-18 10:31:29.450000'): 11,
  Timestamp('2017-08-18 10:42:42.320000'): 11,
  Timestamp('2017-08-18 10:53:34.495000'): 11,
  Timestamp('2017-08-29 09:38:10.660000'): 11,
  Timestamp('2017-08-29 09:42:50.701000'): 11,
  Timestamp('2017-08-29 09:45:21.301000'): 11,
  Timestamp('2017-09-04 09:34:11.032000'): 11,
  Timestamp('2017-09-07 09:34:48.306000'): 11,
  Timestamp('2017-09-07 09:45:30.120000'): 11,
  Timestamp('2017-09-11 09:16:19.693000'): 11,
  Timestamp('2017-09-11 09:19:50.156000'): 11,
  Timestamp('2017-09-11 09:30:40.390000'): 11},
 3: {Timestamp('2017-04-05 10:18:02.095000'): 3,
  Timestamp('2017-04-05 10:35:03.740000'): 3,
  Timestamp('2017-04-05 10:57:18.364000'): 3,
  Timestamp('2017-04-05 11:10:09.142000'): 3,
  Timestamp('2017-04-07 09:41:11.167000'): 3,
  Timestamp('2017-04-07 09:47:22.457000'): 3,
  Timestamp('2017-04-07 09:51:22.037000'): 3,
  Timestamp('2017-04-07 09:54:59.803000'): 3,
  Timestamp('2017-04-07 09:58:49.512000'): 3,
  Timestamp('2017-04-07 10:05:45.506000'): 3,
  Timestamp('2017-04-07 10:06:38.567000'): 3,
  Timestamp('2017-04-24 09:32:06.261000'): 3,
  Timestamp('2017-05-10 09:36:56.943000'): 7,
  Timestamp('2017-05-29 09:31:32.211000'): 7,
  Timestamp('2017-06-19 09:33:56.391000'): 7,
  Timestamp('2017-06-19 09:36:11.743000'): 7,
  Timestamp('2017-06-28 10:06:58.320000'): 10,
  Timestamp('2017-06-28 10:12:04.859000'): 10,
  Timestamp('2017-07-10 09:58:36.082000'): 10,
  Timestamp('2017-07-11 09:43:03.421000'): 10,
  Timestamp('2017-07-12 09:27:27.504000'): 12,
  Timestamp('2017-07-12 09:31:16.304000'): 12,
  Timestamp('2017-07-12 09:32:47.592000'): 12,
  Timestamp('2017-07-12 09:33:43.216000'): 12,
  Timestamp('2017-07-26 09:19:23.656000'): 12,
  Timestamp('2017-07-26 09:32:07.647000'): 12,
  Timestamp('2017-07-26 09:34:16.047000'): 12,
  Timestamp('2017-07-26 09:36:29.241000'): 12,
  Timestamp('2017-07-26 09:41:14.152000'): 12,
  Timestamp('2017-07-26 09:45:01.198000'): 12,
  Timestamp('2017-07-26 09:49:06.674000'): 12,
  Timestamp('2017-08-07 09:17:59.231000'): 12,
  Timestamp('2017-08-07 09:25:57.865000'): 12,
  Timestamp('2017-08-07 09:31:29.751000'): 12,
  Timestamp('2017-08-07 09:35:27.062000'): 12,
  Timestamp('2017-08-15 09:23:40.111000'): 12,
  Timestamp('2017-08-16 09:17:48.032000'): 12,
  Timestamp('2017-08-16 09:20:37.396000'): 12,
  Timestamp('2017-08-16 09:26:34.631000'): 12,
  Timestamp('2017-08-16 10:01:35.525000'): 12,
  Timestamp('2017-08-16 10:06:06.222000'): 12,
  Timestamp('2017-08-16 10:38:44.717000'): 12,
  Timestamp('2017-08-17 09:17:18.951000'): 12,
  Timestamp('2017-08-17 09:21:33.846000'): 12,
  Timestamp('2017-08-17 09:28:14.337000'): 12,
  Timestamp('2017-08-17 09:30:42.855000'): 12,
  Timestamp('2017-08-17 09:31:35.894000'): 12,
  Timestamp('2017-08-17 09:33:15.819000'): 12,
  Timestamp('2017-08-17 09:35:23.751000'): 12,
  Timestamp('2017-08-17 09:38:45.211000'): 12,
  Timestamp('2017-08-17 09:41:25.251000'): 12,
  Timestamp('2017-08-17 09:45:21.319000'): 12,
  Timestamp('2017-08-17 09:47:55.097000'): 12,
  Timestamp('2017-08-17 09:50:17.234000'): 12,
  Timestamp('2017-08-17 09:51:49.333000'): 12,
  Timestamp('2017-08-18 10:13:44.958000'): 13,
  Timestamp('2017-08-18 10:19:25.371000'): 13,
  Timestamp('2017-08-18 10:25:33.984000'): 13,
  Timestamp('2017-08-18 10:31:29.450000'): 13,
  Timestamp('2017-08-18 10:42:42.320000'): 13,
  Timestamp('2017-08-18 10:53:34.495000'): 13,
  Timestamp('2017-08-29 09:38:10.660000'): 13,
  Timestamp('2017-08-29 09:42:50.701000'): 13,
  Timestamp('2017-08-29 09:45:21.301000'): 13,
  Timestamp('2017-09-04 09:34:11.032000'): 13,
  Timestamp('2017-09-07 09:34:48.306000'): 13,
  Timestamp('2017-09-07 09:45:30.120000'): 13,
  Timestamp('2017-09-11 09:16:19.693000'): 13,
  Timestamp('2017-09-11 09:19:50.156000'): 13,
  Timestamp('2017-09-11 09:30:40.390000'): 13},
 4: {Timestamp('2017-04-05 10:18:02.095000'): 4,
  Timestamp('2017-04-05 10:35:03.740000'): 4,
  Timestamp('2017-04-05 10:57:18.364000'): 4,
  Timestamp('2017-04-05 11:10:09.142000'): 4,
  Timestamp('2017-04-07 09:41:11.167000'): 4,
  Timestamp('2017-04-07 09:47:22.457000'): 4,
  Timestamp('2017-04-07 09:51:22.037000'): 4,
  Timestamp('2017-04-07 09:54:59.803000'): 4,
  Timestamp('2017-04-07 09:58:49.512000'): 4,
  Timestamp('2017-04-07 10:05:45.506000'): 4,
  Timestamp('2017-04-07 10:06:38.567000'): 4,
  Timestamp('2017-04-24 09:32:06.261000'): 4,
  Timestamp('2017-05-10 09:36:56.943000'): 8,
  Timestamp('2017-05-29 09:31:32.211000'): 8,
  Timestamp('2017-06-19 09:33:56.391000'): 8,
  Timestamp('2017-06-19 09:36:11.743000'): 8,
  Timestamp('2017-06-28 10:06:58.320000'): 11,
  Timestamp('2017-06-28 10:12:04.859000'): 11,
  Timestamp('2017-07-10 09:58:36.082000'): 11,
  Timestamp('2017-07-11 09:43:03.421000'): 11,
  Timestamp('2017-07-12 09:27:27.504000'): 13,
  Timestamp('2017-07-12 09:31:16.304000'): 13,
  Timestamp('2017-07-12 09:32:47.592000'): 13,
  Timestamp('2017-07-12 09:33:43.216000'): 13,
  Timestamp('2017-07-26 09:19:23.656000'): 13,
  Timestamp('2017-07-26 09:32:07.647000'): 13,
  Timestamp('2017-07-26 09:34:16.047000'): 13,
  Timestamp('2017-07-26 09:36:29.241000'): 13,
  Timestamp('2017-07-26 09:41:14.152000'): 13,
  Timestamp('2017-07-26 09:45:01.198000'): 13,
  Timestamp('2017-07-26 09:49:06.674000'): 13,
  Timestamp('2017-08-07 09:17:59.231000'): 13,
  Timestamp('2017-08-07 09:25:57.865000'): 13,
  Timestamp('2017-08-07 09:31:29.751000'): 13,
  Timestamp('2017-08-07 09:35:27.062000'): 13,
  Timestamp('2017-08-15 09:23:40.111000'): 14,
  Timestamp('2017-08-16 09:17:48.032000'): 14,
  Timestamp('2017-08-16 09:20:37.396000'): 14,
  Timestamp('2017-08-16 09:26:34.631000'): 14,
  Timestamp('2017-08-16 10:01:35.525000'): 14,
  Timestamp('2017-08-16 10:06:06.222000'): 14,
  Timestamp('2017-08-16 10:38:44.717000'): 14,
  Timestamp('2017-08-17 09:17:18.951000'): 14,
  Timestamp('2017-08-17 09:21:33.846000'): 14,
  Timestamp('2017-08-17 09:28:14.337000'): 14,
  Timestamp('2017-08-17 09:30:42.855000'): 14,
  Timestamp('2017-08-17 09:31:35.894000'): 14,
  Timestamp('2017-08-17 09:33:15.819000'): 14,
  Timestamp('2017-08-17 09:35:23.751000'): 14,
  Timestamp('2017-08-17 09:38:45.211000'): 14,
  Timestamp('2017-08-17 09:41:25.251000'): 14,
  Timestamp('2017-08-17 09:45:21.319000'): 14,
  Timestamp('2017-08-17 09:47:55.097000'): 14,
  Timestamp('2017-08-17 09:50:17.234000'): 14,
  Timestamp('2017-08-17 09:51:49.333000'): 14,
  Timestamp('2017-08-18 10:13:44.958000'): 14,
  Timestamp('2017-08-18 10:19:25.371000'): 14,
  Timestamp('2017-08-18 10:25:33.984000'): 14,
  Timestamp('2017-08-18 10:31:29.450000'): 14,
  Timestamp('2017-08-18 10:42:42.320000'): 14,
  Timestamp('2017-08-18 10:53:34.495000'): 14,
  Timestamp('2017-08-29 09:38:10.660000'): 14,
  Timestamp('2017-08-29 09:42:50.701000'): 14,
  Timestamp('2017-08-29 09:45:21.301000'): 14,
  Timestamp('2017-09-04 09:34:11.032000'): 14,
  Timestamp('2017-09-07 09:34:48.306000'): 14,
  Timestamp('2017-09-07 09:45:30.120000'): 14,
  Timestamp('2017-09-11 09:16:19.693000'): 14,
  Timestamp('2017-09-11 09:19:50.156000'): 14,
  Timestamp('2017-09-11 09:30:40.390000'): 14}}

现在我使用for循环来创建新的 Dataframe (result),但是有没有一种更有效的方法可以不使用for循环来实现呢?

df_from = pd.DataFrame(np.random.normal(0,1,size = (70,15)),index = df_ticks_paths.index)

result = pd.DataFrame(np.nan, index = df_from.index, columns = df_from.columns)
    for idx, row in df_path.iterrows():
        for col, col_copy in row.iteritems():
            result.loc[idx,col] = df_from.loc[idx, col_copy]
ktca8awb

ktca8awb1#

这里有另一种方法,在我的机器上,平均 * 速度是 * 的两倍。
第一个
然后道:
第一个

相关问题