基于不同数据类型筛选数据帧

2024-09-30 01:23:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从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_valuesk_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]

Tags: dfstringtypeonealgorithmlongintrow
1条回答
网友
1楼 · 发布于 2024-09-30 01:23:52

由于一些误解,我设法找到了一个不完整的解决方案。请让我知道如何使此解决方案满足您的需要:

t_df = df.T
names = t_df.loc['name']
dtypes = t_df.loc['type']
t_df.columns =  names
t_df = t_df.iloc[2:]
dtype_conv = {'string':str,'int32_t':int}
for dtype,name in zip(dtypes,names):
    t_df[name] = t_df[name].str.split(';')
    t_df=t_df.explode(name)
    t_df[name]  =t_df[name].astype(dtype_conv[dtype])
t_df.sort_values('threads').reset_index(drop=True)

输出:

algorithm   threads
0   one     1
1   two     1
2   three   1
3   one     2
4   two     2
5   three   2
6   one     3
7   two     3
...

相关问题 更多 >

    热门问题