我在pandas df中的一列包含代表一组的字符串
hp_row = hp_df.loc[set(eval(hp_df['tables'].to_numpy())) == {school.csv'}]
我试图找到与特定设置值相对应的行,如下所示:
ValueError: source code string cannot contain null bytes
但这显然行不通。
"{'school.csv'}",1024,16,4,8
是否有正确方法的帮助?
hp_row的预期值为
for i in range(df.shape[0]):
row = df.iloc[i]
s = set(eval(row['tables']))
if s == {"school.csv"}:
selected_row = row
请注意,我要比较集合对象,而不是字符串。
谢谢
编辑: 我的临时解决方案(但正在寻找更紧凑和优化的东西):
json baselineOpenAndRead(string fileName) //
{
json baseJObject;
cout << "we have a baseJObject" << endl;
string filePath = "../baselines/" + fileName;
cout << "filePath: " << filePath << endl;
//ifstream inFileJSON(filePath.c_str());
ifstream inFileJSON(filePath);
if (inFileJSON.is_open())
{
cout << "File is open." << endl;
inFileJSON >> baseJObject;
cout << baseJObject << std::endl;
inFileJSON.close();
return baseJObject;
}
else
{
cout << "File not open." << endl;
exit(1);
}
}
答案 0 :(得分:2)
IIUC,您可以使用ast.literal_eval
将列table
从字符串转换为集合并将其分配给s
。接下来,将loc
与s
一起使用来切片
import ast
s = df.tables.apply(ast.literal_eval)
df.loc[s == {'school.csv'}]
Out[109]:
tables n_estimators min_samples_split min_samples_leaf \
0 {'school.csv'} 1024 16 4
max_depth
0 8
答案 1 :(得分:0)
如果您想对包含需求元素的set行进行切片:
hp_df[hp_df.apply(lambda row: 'school.csv' in eval(row['tables']),axis=1)]