[英]Dropping column from one dataframe based on column value of second dataframe in pandas
I have 2 dataframes df1 and df2, both consisting of 8 columns each as seen below : 我有2个数据帧df1和df2,都由8列组成,如下所示:
**df1**
╔══════════════════════════════════════════════════════════╗
║John ║ Mark ║ Jane ║ Natasha ║ Oliver ║ Tony ║ Judd ║ Ron ║
╚══════════════════════════════════════════════════════════╝
**df2**
╔══════════════════════════════════════════════════╗
║True ║True ║False ║True ║False ║False ║False ║True║
╚══════════════════════════════════════════════════╝
df1 has columns that are names of different people while df2 has column names that are boolean values. df1的列是不同人的名字,而df2的列名是布尔值。 What I want to do is drop all columns in df1 that have a corresponding value of False in df2 . 我想做的是删除df1中所有在df2中具有False值的列。 So the resulting output should look like this : 因此,结果输出应如下所示:
**output**
╔════════════════════════════╗
║John ║ Mark ║ Natasha ║ Ron ║
╚════════════════════════════╝
I am reading both the dataframes from csv files. 我正在从csv文件读取两个数据帧。
Any and all help would be appreciated. 任何和所有帮助将不胜感激。
Note : The actual dataframes have 500 columns each. 注意 :实际的数据帧每个都有500列。 Used 8 as an example for visualization purposes as well as to show that the dataframes have equal number of columns 使用8作为示例进行可视化,并显示数据框具有相等的列数
Thanks in advance 提前致谢
You can, using basic indexing. 您可以使用基本索引。 However, when you parse your df2
, the column names have duplicates and are altered, so it requires a bit of cleaning. 但是,当您解析df2
,列名重复且已更改,因此需要进行一些清理。
Setup 设定
names = ['John', 'Mark', 'Jane', 'Natasha', 'Oliver', 'Tony', 'Judd', 'Ron']
cols = ['TRUE', 'TRUE.1', 'FALSE', 'FALSE.1', 'TRUE.2', 'FALSE.2', 'FALSE.3', 'TRUE.3']
df1 = pd.DataFrame(columns=names)
df2 = pd.DataFrame(columns=cols)
df1.loc[:, df2.columns.str.contains('TRUE')]
Empty DataFrame
Columns: [John, Mark, Oliver, Ron]
Index: []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.