我知道,這似乎是一個很容易回答的問題,但是,我只是被困在是否有辦法做到這一點。
我有一個 DataFrame(帶索引),我在該框架中插入了一個新列,該列能夠每 10 行分組一次,并且每個組都有從 1 到 ... 的數字。我使用了這個非常基本的代碼,它奏效了!
df1.insert(0, 'Data', (df.index // 10) 1)
問題是;現在,我有一個 NumPy 陣列(unit8),其中不包含索引,這就是為什么上面的代碼不適用于相同條件的原因。我想做同樣的事情,代碼將每 10 行計數一次,將它們分組,并在新添加的列中為每個組添加一個數字。
uj5u.com熱心網友回復:
我不確定我是否理解您的問題(也許您能舉一個您正在使用的代碼示例)。無論如何,我認為一個可能的解決方案可能是將您的陣列轉換為只有一列的資料框(現在您有索引),然后應用您的公式:
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
uj5u.com熱心網友回復:
使用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.]])
uj5u.com熱心網友回復:
因此,如果我理解您的問題,那么您必須將 acolumn 添加到您的(大概)一維陣列中。
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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348720.html
標籤:熊猫 数据框 麻木 numpy-ndarray
