简体   繁体   English

根据熊猫中第二个数据框的列值从一个数据框删除列

[英]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.

相关问题 根据列中的值从熊猫数据框中删除行 - Dropping rows from pandas dataframe based on value in column(s) 使用 Pandas,根据第二列的最小值从数据框中的一列(对于每组)获取值 - With Pandas, get value from one column in dataframe (for each group), based on minimum value of second column 熊猫-根据Datetime列值删除DataFrame行 - Pandas - Dropping DataFrame rows based on Datetime column value 根据列名删除 Pandas Dataframe 列 - Dropping Pandas Dataframe Columns based on column name 根据 Pandas 中的 id 将列值从一个数据帧复制到另一个数据帧 - Copy column value from one dataframe to another based on id in Pandas 根据列值将数据从一个 Pandas 数据帧复制到另一个 - Copying data from one pandas dataframe to other based on column value 从熊猫数据框中删除“垃圾”列 - Dropping a "garbage" column from pandas dataframe 根据来自不同列 Pandas Dataframe 的值创建列 - Create column based on value from different column Pandas Dataframe Pandas 合并数据帧并仅从第二个 Dataframe 更新一列 - Pandas merge dataframes and update Only one Column from Second Dataframe 根据 pandas dataframe 中的另一列从一列中获取值时返回空数组 - Empty array returned while fetching value from one column based on another column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM