根据行提取具有不同索引列的熊猫数据框的值

时间:2019-01-07 09:51:50

标签: python pandas list

我有这个数据框,其中包含 1 000 000行和1 00列

   0       1          2         3         4         5         6          ...
0  2.645751  2.828427  3.000000  3.000000  3.000000  3.000000  3.000000   
1  2.645751  2.828427  2.828427  3.000000  3.000000  3.000000  3.000000   
2  2.449490  2.449490  2.645751  2.645751  2.645751  2.645751  2.645751   
3  2.000000  2.236068  2.449490  2.449490  2.449490  2.449490  2.449490   
4  2.449490  2.828427  2.828427  2.828427  2.828427  2.828427  2.828427   
5  1.414214  1.414214  1.414214  1.414214  1.414214  1.414214  1.732051

可复制示例(将其转换为df):

df={0: {0: 2.6457513110645907, 1: 2.6457513110645907},
 1: {0: 2.8284271247461903, 1: 2.8284271247461903},
 2: {0: 3.0, 1: 2.8284271247461903},
 3: {0: 3.0, 1: 3.0},
 4: {0: 3.0, 1: 3.0},
 5: {0: 3.0, 1: 3.0},
 6: {0: 3.0, 1: 3.0},
 7: {0: 3.0, 1: 3.0},
 8: {0: 3.0, 1: 3.0},
 9: {0: 3.0, 1: 3.0},
 10: {0: 3.0, 1: 3.0},
 11: {0: 3.0, 1: 3.0},
 12: {0: 3.0, 1: 3.0},
 13: {0: 3.0, 1: 3.0},
 14: {0: 3.0, 1: 3.0},
 15: {0: 3.0, 1: 3.0},
 16: {0: 3.0, 1: 3.0},
 17: {0: 3.1622776601683795, 1: 3.0},
 18: {0: 3.1622776601683795, 1: 3.0},
 19: {0: 3.1622776601683795, 1: 3.0},
 20: {0: 3.1622776601683795, 1: 3.0},
 21: {0: 3.1622776601683795, 1: 3.0},
 22: {0: 3.1622776601683795, 1: 3.0},
 23: {0: 3.1622776601683795, 1: 3.0},
 24: {0: 3.1622776601683795, 1: 3.0},
 25: {0: 3.1622776601683795, 1: 3.0},
 26: {0: 3.1622776601683795, 1: 3.1622776601683795},
 27: {0: 3.1622776601683795, 1: 3.1622776601683795},
 28: {0: 3.1622776601683795, 1: 3.1622776601683795},
 29: {0: 3.1622776601683795, 1: 3.1622776601683795},
 30: {0: 3.1622776601683795, 1: 3.1622776601683795},
 31: {0: 3.1622776601683795, 1: 3.1622776601683795},
 32: {0: 3.1622776601683795, 1: 3.1622776601683795},
 33: {0: 3.1622776601683795, 1: 3.3166247903554},
 34: {0: 3.1622776601683795, 1: 3.3166247903554},
 35: {0: 3.1622776601683795, 1: 3.3166247903554},
 36: {0: 3.3166247903554, 1: 3.3166247903554},
 37: {0: 3.3166247903554, 1: 3.3166247903554},
 38: {0: 3.3166247903554, 1: 3.3166247903554},
 39: {0: 3.3166247903554, 1: 3.3166247903554},
 40: {0: 3.3166247903554, 1: 3.3166247903554},
 41: {0: 3.3166247903554, 1: 3.3166247903554},
 42: {0: 3.3166247903554, 1: 3.3166247903554},
 43: {0: 3.3166247903554, 1: 3.3166247903554},
 44: {0: 3.3166247903554, 1: 3.3166247903554},
 45: {0: 3.3166247903554, 1: 3.3166247903554},
 46: {0: 3.3166247903554, 1: 3.3166247903554},
 47: {0: 3.3166247903554, 1: 3.3166247903554},
 48: {0: 3.3166247903554, 1: 3.3166247903554},
 49: {0: 3.3166247903554, 1: 3.3166247903554},
 50: {0: 3.3166247903554, 1: 3.3166247903554},
 51: {0: 3.3166247903554, 1: 3.3166247903554},
 52: {0: 3.3166247903554, 1: 3.3166247903554},
 53: {0: 3.3166247903554, 1: 3.3166247903554},
 54: {0: 3.3166247903554, 1: 3.3166247903554},
 55: {0: 3.3166247903554, 1: 3.3166247903554},
 56: {0: 3.3166247903554, 1: 3.3166247903554},
 57: {0: 3.3166247903554, 1: 3.3166247903554},
 58: {0: 3.3166247903554, 1: 3.3166247903554},
 59: {0: 3.3166247903554, 1: 3.3166247903554},
 60: {0: 3.3166247903554, 1: 3.3166247903554},
 61: {0: 3.3166247903554, 1: 3.3166247903554},
 62: {0: 3.3166247903554, 1: 3.3166247903554},
 63: {0: 3.3166247903554, 1: 3.3166247903554},
 64: {0: 3.3166247903554, 1: 3.3166247903554},
 65: {0: 3.3166247903554, 1: 3.3166247903554},
 66: {0: 3.3166247903554, 1: 3.3166247903554},
 67: {0: 3.3166247903554, 1: 3.3166247903554},
 68: {0: 3.3166247903554, 1: 3.3166247903554},
 69: {0: 3.3166247903554, 1: 3.3166247903554},
 70: {0: 3.3166247903554, 1: 3.3166247903554},
 71: {0: 3.3166247903554, 1: 3.3166247903554},
 72: {0: 3.3166247903554, 1: 3.3166247903554},
 73: {0: 3.3166247903554, 1: 3.3166247903554},
 74: {0: 3.3166247903554, 1: 3.3166247903554},
 75: {0: 3.3166247903554, 1: 3.3166247903554},
 76: {0: 3.3166247903554, 1: 3.3166247903554},
 77: {0: 3.3166247903554, 1: 3.3166247903554},
 78: {0: 3.3166247903554, 1: 3.3166247903554},
 79: {0: 3.3166247903554, 1: 3.3166247903554},
 80: {0: 3.3166247903554, 1: 3.3166247903554},
 81: {0: 3.3166247903554, 1: 3.3166247903554},
 82: {0: 3.3166247903554, 1: 3.3166247903554},
 83: {0: 3.3166247903554, 1: 3.3166247903554},
 84: {0: 3.3166247903554, 1: 3.3166247903554},
 85: {0: 3.3166247903554, 1: 3.3166247903554},
 86: {0: 3.3166247903554, 1: 3.3166247903554},
 87: {0: 3.3166247903554, 1: 3.3166247903554},
 88: {0: 3.3166247903554, 1: 3.3166247903554},
 89: {0: 3.3166247903554, 1: 3.3166247903554},
 90: {0: 3.3166247903554, 1: 3.3166247903554},
 91: {0: 3.3166247903554, 1: 3.3166247903554},
 92: {0: 3.3166247903554, 1: 3.3166247903554},
 93: {0: 3.3166247903554, 1: 3.3166247903554},
 94: {0: 3.3166247903554, 1: 3.3166247903554},
 95: {0: 3.3166247903554, 1: 3.3166247903554},
 96: {0: 3.3166247903554, 1: 3.3166247903554},
 97: {0: 3.3166247903554, 1: 3.3166247903554},
 98: {0: 3.3166247903554, 1: 3.3166247903554},
 99: {0: 3.3166247903554, 1: 3.3166247903554}}

我有一个具有不同长度的列表,其中包含我需要的列的索引。

list_idx = [[array([ 7, 12, 49])], [array([ 4, 34, 41, 45, 80, 82])]]

list_idx ([array([7,12,49])]) 的第一个元素是要为第一行提取的值。             <-> 第1行:我需要数据框的第7、12和49列的值。

这里有执行此操作的代码,但是有没有更快的方法来提取值?

finalListofList=[
for (row,idx) in zip(df.iterrows(),list_idx ):
   finalListofList.append(list(row[1][idx[0]]))

2 个答案:

答案 0 :(得分:2)

您可以简单地使用DataFrame.loc

finalListofList = df.loc[0,list_idx[0][0]].values
# array([3.        , 3.        , 3.31662479])

请注意,[0]中多余的list_idx[0][0]是因为您有一个嵌套列表,即list_idx[0]仍然提供了在这种情况下无法索引的列表。

您可以阅读有关建立索引和选择数据here

的更多信息

答案 1 :(得分:1)

在列表理解中使用numpy索引:

finalListofList = [row[idx[0]].tolist() for row, idx in zip(df.values, list_idx)]