我有一个示例熊猫数据框,如下所示:
delme = {
"c1": [1, 2, 3],
"c2": ["he", "she", "he"],
"c3": [1.3, 1.4, 1.5],
"pre_c1": ["x1", "x2", "x3"],
"pre_c2": ["y1", "y2", "y3"],
"pre_c3": ["z1", "z2", "z3"],
"npre_c1": ["x1", "x2", "x3"],
"npre_c2": ["y1", "y2", "y3"]
}
delmedf = pd.DataFrame(delme)
c1 c2 c3 pre_c1 pre_c2 pre_c3 npre_c1 npre_c2
1 he 1.3 x1 y1 z1 x1 y1
2 she 1.4 x2 y2 z2 x2 y2
3 he 1.5 x3 y3 z3 x3 y3
我想从pre_
数据框中选择所有带有前缀npre_
和c3
的列以及列delmedf
。我该怎么办?
到目前为止,我已尝试分别捕获它们,然后将它们与axis=1
合并,如下所示:
df1 = delmedf[delmedf.columns[(pd.Series(delmedf.columns).str.contains("pre_"))]]
df2= delmedf[[c2]]
data_X = pd.concat([df1, df2], axis=1)
但是,只要我想引入另一个str.contains("npre_")
条件,上述方法就会失败:
df1 = delmedf[delmedf.columns[(pd.Series(delmedf.columns).str.contains("pre_")) or (pd.Series(delmedf.columns).str.contains("npre_"))]]
上面的语句出现的错误是:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
谢谢。
答案 0 :(得分:2)
使用:
m=delmedf.columns.str.contains('pre_')|(delmedf.columns=='c3')
delmedf[delmedf.columns[m]]
输出
c3 pre_c1 pre_c2 pre_c3 npre_c1 npre_c2
0 1.3 x1 y1 z1 x1 y1
1 1.4 x2 y2 z2 x2 y2
2 1.5 x3 y3 z3 x3 y3
答案 1 :(得分:0)
您可以使用这样的正则表达式:
df.loc[:,np.ndarray.flatten(df.columns.str.extract(r'(^[n]?pre_.*|c3)').dropna().to_numpy())]
c3 pre_c1 pre_c2 pre_c3 npre_c1 npre_c2
0 1.3 x1 y1 z1 x1 y1
1 1.4 x2 y2 z2 x2 y2
2 1.5 x3 y3 z3 x3 y3
答案 2 :(得分:0)
还有一个选择:)
MySql.Data.MySqlClient.MySqlException: 'Authentication to host 'ip' for user 'root' using method 'mysql_native_password' failed with message: Access denied for user 'root'@'my-ip' (using password: YES)'
结果:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(void)
{
string message=get_string("Enter a message to encrypt: \n");
int key=get_int("Enter a key to encrypt the message: \n");
char m[strlen(message)];
strcpy(m, message);
for(int i=0; i<strlen(message); i++)
{
char letter=m[i];
if(isalpha(m[i])||isupper(m[i])||islower(m[i]))
{
if(letter>='a'&&letter<='z')
{
letter=letter+key;
if(letter>'z')
{
letter=letter-'z'+'a'-1;
letter=m[i];
}
letter=m[i];
}
else if(letter>='A'&&letter<='Z')
{
letter=letter+key;
if(letter>'Z')
{
letter=letter-'Z'+'A'-1;
letter=m[i];
}
letter=m[i];
}
}
printf("%c", m[i]);
}
printf("\n");
return 0;
}