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
↓
↓
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.)
Column containing P1 in column name is https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html Therefore, label is one, and the column name contains P1, and the column name contains P3 is one or more elements. Call it as shown in . However, in this case, it would be less appropriate to use https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.startswith.html Furthermore, if the column names are known, it may simply be Slice.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.any.htmldf[(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]
df.columns.str.startswith('P1')
as "columns whose column name starts with P1" rather than "columns with P1df[(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
df[(df.loc[:, 'label']=1)&(df.loc[:, 'P1S1': 'P1S3']>=1).any(axis=1)&(df.loc[:, 'P3S1': 'P3S3']>=1)
© 2024 OneMinuteCode. All rights reserved.