我正在从csv文件创建初始df,如下所示:
knobs_df = pd.read_csv(knobs_container)
name type values
0 algorithm string one;two;three
1 threads int32_t 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
对于每一行,我将type列和values列作为字典提取到k_values
和k_type
中
k_values = {}
k_types = {}
for row in knobs_df.itertuples(index=False):
k_values[row[0]] = row[2].split(';')
k_types[row[0]] = row[1]
{'algorithm': ['one', 'two', 'three'], 'threads': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']}
{'algorithm': 'string', 'threads': 'int32_t'}
我从k_values
字典生成一个包含所有可能组合的完整网格
algorithm threads
0 one 1
1 two 1
2 three 1
3 one 2
4 two 2
.. ... ...
88 two 14
89 three 14
90 one 15
91 two 15
92 three 15
具有如下所示的约束(Python表达式)列表
['threads < 20', 'algorithm != "two"']
我想使用pandas.DataFrame
中的query
方法过滤完整的网格数据帧。是否有一种方法可以根据k_types
字典为每一列分配相应的数据类型?我之所以需要这样做,是因为每个列都可能有一个独立的类型,例如,查询方法在筛选“threads”列时失败,因为在创建过程中,所有列都默认推断为“str”。问题是,由于最初是C++数据类型,我不知道是否有办法实现这个。p>
可能的k_类型包括:
[string, short int, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t, char, int, long int, long long int, int_fast8_t, int_fast16_t, int_fast32_t, int_fast64_t, int_least8_t, int_least_16_t, int_least32_t, int_least64_t, unsigned short int, unsigned char, unsigned int, unsigned long int, unsigned long long int, uint_fast8_t, uint_fast16_t, uint_fast32_t, uint_fast64_t, uint_least8_t, uint_least16_t, uint_least32_t, uint_least64_t, intmax_t, intptr_t, uintmax_t, uintptr_t, float, double, long double]
由于一些误解,我设法找到了一个不完整的解决方案。请让我知道如何使此解决方案满足您的需要:
输出:
相关问题 更多 >
编程相关推荐