Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is invalid key Pandas Python

I have a dataframe in Pandas with 729278 rows and 190 columns:

df1:

+----------+----------+----------+---+---+-----+---------+
| RULE_1_2 | RULE_2_2 | RULE_3_2 | … | … | smt | default |
+----------+----------+----------+---+---+-----+---------+
| 0        | 0        | 0        | … | … | 2   | 0       |
| 0        | 2        | 3        | … | … | 3   | 0       |
| 1        | 3        | 0        | … | … | 4   | 1       |
| …        | …        | …        | … | … | …   | …       |
+----------+----------+----------+---+---+-----+---------+

Trying to exctract all columns containing RULE and column 'default'.

Code:

df2 = df1[df1.filter(regex='RULE'), df1["default"]]

But Python says:

[729278 rows x 1 columns])' is an invalid key

All columns contain int64 type, which confirmed by df1.dtypes

What's wrong with 1 column 'default'? It doesn't appear in datamrame 'df2'. How to fix it?

like image 241
gehr-sao Avatar asked Jun 05 '26 19:06

gehr-sao


1 Answers

Idea is add another part of regex joined by | for regex or, also ^ is for start of string and $ for end of string for prevent selecting strings like some data default:

df2 = df1.filter(regex='RULE|^default$')
like image 174
jezrael Avatar answered Jun 07 '26 08:06

jezrael