Pandas:创建新列,交换其他行的值

时间:2017-02-05 15:38:55

标签: python pandas

我正在尝试创建像这样的pandas数据框:

           x2        x3
0    3.536220  0.681269
1    0.681269  3.536220
2   -0.402380  2.303833
3    2.303833 -0.402380
4    2.032329  3.334412
5    3.334412  2.032329
6    0.371338  5.879732
. . .

因此x2是一列随机数,x3的值为x2中的行0和1,交换的值为2和3,依此类推。我目前的代码是这样的:

import numpy as np
import pandas as pd
x2 = pd.Series(np.random.normal(loc = 2, scale = 2.5, size = 1000))
x3 = pd.Series([x2[i + 1] if i % 2 == 0 else x2[i - 1] for i in range(1000)])
df = pd.DataFrame({'x2': x2, 'x3': x3})

我想知道是否有更快或更优雅的方式,特别是如果我想要有很多行(例如100万?)或反复这样做(例如蒙特卡罗模拟)?

1 个答案:

答案 0 :(得分:6)

而不是

  // This is the counter
  var indentification = 0;

  // This is the submit button
  var submit = document.getElementById("submit");

  // This is the text field
  var content = document.getElementById("text");

  submit.onclick = function() {
      id = indentification++;
      description = content.value;

      var task = {
          list: []
      }

      task.list.push({id, description});
      var jsonifyTask = JSON.stringify(task);
      fs.writeFile("tasks.json", jsonifyTask, "utf8");
  }

你可以使用

[x2[i + 1] if i % 2 == 0 else x2[i - 1] for i in range(1000)]

对于长度为1000的序列,使用def swap(arr): result = np.empty_like(arr) result[::2] = arr[1::2] result[1::2] = arr[::2] return result 的速度提高了3000倍以上:

swap
In [84]: %timeit [x2[i + 1] if i % 2 == 0 else x2[i - 1] for i in range(1000)]
100 loops, best of 3: 12.7 ms per loop

In [98]: %timeit  swap(x2.values)
100000 loops, best of 3: 3.82 µs per loop

打印

import numpy as np
import pandas as pd
np.random.seed(2017)
x2 = pd.Series(np.random.normal(loc = 2, scale = 2.5, size = 1000))
x3 = [x2[i + 1] if i % 2 == 0 else x2[i - 1] for i in range(1000)]

def swap(arr):
    result = np.empty_like(arr)
    result[::2] = arr[1::2]
    result[1::2] = arr[::2]
    return result

df = pd.DataFrame({'x2': x2, 'x3': x3, 'x4': swap(x2.values)})
print(df.head())