How to extract elements containing 〇 を in the column name of Pandas dataframe and filter the values further

Asked 2 years ago, Updated 2 years ago, 49 views


for dataframe below
Exclude elements with label 1 and column name containing P1 and column name containing P3 and column name containing 1 or more.
I'd like to add this action.

Could you give me an example of Python code?

dataframe
Input data


Output data
(Other than the one with label 0 we did not extract any column names that contain P1 and P3 with all 0 values.)
Enter a description of the image here

python pandas

2022-09-30 17:06

1 Answers

Column containing P1 in column name is df.columns.str.contains('P1'), so (df.loc[:,df.columns.str.contains(') where column name contains at least one value is (df.loc[:)=1/code.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.any.html

Therefore, label is one, and the column name contains P1, and the column name contains P3 is one or more elements.

df[(df.loc[:, 'label']==1)&(df.loc[:,df.columns.str.contains('P1')]>=1).any(axis=1)&(df.loc[:,df.columns.str.contains('P3')=1]&giny=1]

Call it as shown in .

However, in this case, it would be less appropriate to use df.columns.str.startswith('P1') as "columns whose column name starts with P1" rather than "columns with P1

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.startswith.html

df[(df.loc[:, 'label']==1)&(df.loc[:,df.columns.str.startswith('P1')]>=1).any(axis=1)&(df.loc[:,df.columns.str.startswith('Py3)=1]&gasy]=1

Furthermore, if the column names are known, it may simply be Slice.

df[(df.loc[:, 'label']=1)&(df.loc[:, 'P1S1': 'P1S3']>=1).any(axis=1)&(df.loc[:, 'P3S1': 'P3S3']>=1)


2022-09-30 17:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.