使用MultiIndex在DataFrame上建立索引

JNevens

我有一个大熊猫DataFrame需要填充。

这是我的代码:

trains = np.arange(1, 101) 
#The above are example values, it's actually 900 integers between 1 and 20000
tresholds = np.arange(10, 70, 10)
tuples = []
for i in trains:
    for j in tresholds:
        tuples.append((i, j))

index = pd.MultiIndex.from_tuples(tuples, names=['trains', 'tresholds'])
df = pd.DataFrame(np.zeros((len(index), len(trains))), index=index, columns=trains, dtype=float)

metrics = dict()
for i in trains:
    m = binary_metric_train(True, i) 
    #Above function returns a binary array of length 35
    #Example: [1, 0, 0, 1, ...]
    metrics[i] = m

for i in trains:
    for j in tresholds:
        trA = binary_metric_train(True, i, tresh=j)
        for k in trains:
            if k != i:
                trB = metrics[k]
                corr = abs(pearsonr(trA, trB)[0])
                df[k][i][j] = corr
            else:
                df[k][i][j] = np.nan

我的问题是,当这段代码最终完成计算后,我的DataFramedf仍然只包含零。即使NaN没有插入。我认为我的索引编制是正确的。另外,我已经分别测试了我的binary_metric_train函数,它确实返回了长度为35的数组。

有人可以在这里发现我的失踪吗?

编辑:为清楚起见,此DataFrame看起来像这样:

                    1   2   3   4   5   ...
trains  tresholds
     1         10
               20
               30
               40
               50
               60
     2         10
               20
               30
               40
               50
               60
   ...
马特

正如@EdChum指出的那样,您应该关注一下pandas索引编制这是一些用于说明目的的测试数据,应将其清除。

import numpy as np
import pandas as pd

trains     = [ 1,  1,  1,  2,  2,  2]
thresholds = [10, 20, 30, 10, 20, 30]
data       = [ 1,  0,  1,  0,  1,  0]
df = pd.DataFrame({
    'trains'     : trains,
    'thresholds' : thresholds,
    'C1'         : data,
    'C2'         : data
}).set_index(['trains', 'thresholds'])

print df
df.ix[(2, 30), 0] = 3 # using column index
# or...
df.ix[(2, 30), 'C1'] = 3 # using column name
df.loc[(2, 30), 'C1'] = 3 # using column name
# but not...
df.loc[(2, 30), 1] = 3 # creates a new column
print df

输出DataFrame修改前和修改后的内容:

                   C1  C2
trains thresholds        
1      10           1   1
       20           0   0
       30           1   1
2      10           0   0
       20           1   1
       30           0   0
                   C1  C2   1
trains thresholds            
1      10           1   1 NaN
       20           0   0 NaN
       30           1   1 NaN
2      10           0   0 NaN
       20           1   1 NaN
       30           3   0   3

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Pandas MultiIndex DataFrame 上建立索引时出现 KeyError

熊猫-在包含间隔的MultiIndex上建立索引

Pandas - 使用 MultiIndex DataFrame 的索引切入 DataFrame

使用MultiIndex索引到Pandas DataFrame时保持顺序吗?

使用基于索引的条件对MultiIndex DataFrame进行切片

在 MultiIndex pandas 数据帧上使用多维索引?

如何在已建立索引的文档上使用提取管道?

Nutch无法使用Mongodb在Elasticsearch上正确建立索引

大熊猫:在MultiIndex DataFrame上复制/广播单索引DataFrame:HowTo和内存效率

如何在MultiIndex DataFrame上使用Pandas query()方法?

使用 MultiIndex 旋转 DataFrame

Pandas Dataframe Mul在Multiindex上

慢熊猫DataFrame MultiIndex重新索引

NSUserActivity没有在OSX上建立索引

在时间戳Firebase上建立索引

在低基数字段上建立索引

在PostgreSQL中的jsonb键上建立索引

使用索引值列表对Pandas MultiIndex DataFrame进行切片

插入一行以使用 python 为 Dataframe 建立索引

如何使用Nginx设置阻止XML在Google on Rails上为XML文件建立索引?

将 DataFrame 对象上的 Pandas 日期时间索引转换为具有“月”和“年”级别的 *MultiIndex*

使用 MultiIndex 在轴上进行基本索引

如何使用for循环为列表建立索引?

使用VBA在Excel中建立索引

使用htaccess防止对PDF文件建立索引

通过另一个包含行和列索引作为列的 DataFrame 使用 MultiIndex Rows 和 Columns 索引 DataFrame

Pandas 1.0.1-如何使用包含切片器的列表为具有MultiIndex的DataFrame编制索引

Python:使用Datetime索引在Pandas中建立索引

列计算中的Pandas MultiIndex DataFrame参考索引值