如何在 NumPy 数组中创建索引列?
How to make an index column in NumPy array?
我知道,这个问题好像很容易回答,但是,我只是纠结于是否有办法做到这一点。
我有一个 DataFrame(带索引),我向该框架插入了一个新列,该列能够每 10 行分组一次,并且每组的数字从 1 到 ...。我使用了这个非常基本的代码并且它起作用了!
df1.insert(0, 'Data', (df.index // 10) + 1)
问题是;现在,我有一个 NumPy 数组 (unit8),其中不包含索引,这就是上面的代码不适用于相同条件的原因。我想做同样的事情,代码将每 10 行计数一次,将它们分组,并在新添加的列中为每个组添加一个数字。
我不确定我是否理解您的问题(也许您可以举一个您正在使用的代码示例)。
无论如何,我认为一个可能的解决方案是将您的数组转换为只有一列的数据框(现在您有了索引),然后应用您的公式:
import pandas as pd
import numpy as np
arr = np.random.normal(size = 100) # just a random array
df = pd.DataFrame(arr, columns = ['arr'])
print(df)
您将获得:
arr
0 -0.834342
1 2.156343
2 -0.527963
3 -0.311767
4 1.029866
.. ...
95 0.047856
96 -1.009195
97 -0.239678
98 0.393085
99 -1.277784
使用np.repeat
:
m = np.arange(1, 24)
n = np.repeat(np.arange(1, np.ceil(len(m) / 10) + 1), 10)[:len(m)]
输出:
>>> np.vstack([n, m]).T
array([[ 1., 1.],
[ 1., 2.],
[ 1., 3.],
[ 1., 4.],
[ 1., 5.],
[ 1., 6.],
[ 1., 7.],
[ 1., 8.],
[ 1., 9.],
[ 1., 10.],
[ 2., 11.],
[ 2., 12.],
[ 2., 13.],
[ 2., 14.],
[ 2., 15.],
[ 2., 16.],
[ 2., 17.],
[ 2., 18.],
[ 2., 19.],
[ 2., 20.],
[ 3., 21.],
[ 3., 22.],
[ 3., 23.]])
所以如果我理解你的问题,那么你必须向你的(大概)一维数组添加一列。
import numpy as np
array = np.random.randint(0, 100,size=100) # random numpy array (1D)
index = np.arange(array.shape[0]) # create index array for indexing
array_with_indices = np.c_[array, index]
array_with indices[:, 1] // 10 + 1 # taking second column as it contains the indices
# or we can convert it to a dataframe if you prefer
df = pd.DataFrame(array, index = index)
# then it should work perfectly
df.index//10 + 1
然后就可以插入df1了。
我知道,这个问题好像很容易回答,但是,我只是纠结于是否有办法做到这一点。
我有一个 DataFrame(带索引),我向该框架插入了一个新列,该列能够每 10 行分组一次,并且每组的数字从 1 到 ...。我使用了这个非常基本的代码并且它起作用了!
df1.insert(0, 'Data', (df.index // 10) + 1)
问题是;现在,我有一个 NumPy 数组 (unit8),其中不包含索引,这就是上面的代码不适用于相同条件的原因。我想做同样的事情,代码将每 10 行计数一次,将它们分组,并在新添加的列中为每个组添加一个数字。
我不确定我是否理解您的问题(也许您可以举一个您正在使用的代码示例)。 无论如何,我认为一个可能的解决方案是将您的数组转换为只有一列的数据框(现在您有了索引),然后应用您的公式:
import pandas as pd
import numpy as np
arr = np.random.normal(size = 100) # just a random array
df = pd.DataFrame(arr, columns = ['arr'])
print(df)
您将获得:
arr
0 -0.834342
1 2.156343
2 -0.527963
3 -0.311767
4 1.029866
.. ...
95 0.047856
96 -1.009195
97 -0.239678
98 0.393085
99 -1.277784
使用np.repeat
:
m = np.arange(1, 24)
n = np.repeat(np.arange(1, np.ceil(len(m) / 10) + 1), 10)[:len(m)]
输出:
>>> np.vstack([n, m]).T
array([[ 1., 1.],
[ 1., 2.],
[ 1., 3.],
[ 1., 4.],
[ 1., 5.],
[ 1., 6.],
[ 1., 7.],
[ 1., 8.],
[ 1., 9.],
[ 1., 10.],
[ 2., 11.],
[ 2., 12.],
[ 2., 13.],
[ 2., 14.],
[ 2., 15.],
[ 2., 16.],
[ 2., 17.],
[ 2., 18.],
[ 2., 19.],
[ 2., 20.],
[ 3., 21.],
[ 3., 22.],
[ 3., 23.]])
所以如果我理解你的问题,那么你必须向你的(大概)一维数组添加一列。
import numpy as np
array = np.random.randint(0, 100,size=100) # random numpy array (1D)
index = np.arange(array.shape[0]) # create index array for indexing
array_with_indices = np.c_[array, index]
array_with indices[:, 1] // 10 + 1 # taking second column as it contains the indices
# or we can convert it to a dataframe if you prefer
df = pd.DataFrame(array, index = index)
# then it should work perfectly
df.index//10 + 1
然后就可以插入df1了。