简体   繁体   English

根据包含字符串的列(2 个定义的条件)从另一个数据帧创建 dataframe

[英]Create a dataframe from another data frame based on column containing string (2 defined conditions)

Iam trying to compile some of the columns in df1 to a new dataframe df2.我正在尝试将 df1 中的一些列编译为新的 dataframe df2。

the columns will be selected based on the following conditions:将根据以下条件选择列:

  1. if word 'COORDINATES' is in the column如果单词 'COORDINATES' 在列中

  2. if word 'ID' is in the column如果单词“ID”在列中

Here is the code I tried:这是我尝试过的代码:

df1 = pd.read_csv(csvpath) #table as below

cols = [col for col in df1.columns if  'Coordinates' and 'ID' in col]

df2=df1[cols]

However the conditions are only being applied for the last item in cols= (in this case its only extracting ID and ignoring coordinates)但是,条件仅适用于 cols= 中的最后一项(在这种情况下,它仅提取 ID 并忽略坐标)

How do i edit the above code to include both Coordinates and ID (I could just drop the unwanted columns however the dataset im dealing with is large and hence i need to do it in such a way similar to what i defined above)我如何编辑上面的代码以包含坐标和 ID(我可以删除不需要的列,但是我处理的数据集很大,因此我需要以类似于我上面定义的方式进行操作)

much appreciated your help on this.非常感谢您对此的帮助。

Original Table (df1)原始表 (df1)

在此处输入图像描述

Required Output(df2)所需输出(df2)

在此处输入图像描述

I think this should work我认为这应该有效

cols = [col for col in df1.columns if  'Coordinates' in col or 'ID' in col]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据来自另一个数据帧的多列条件创建多列 - Create multiple columns based on multiple column conditions from another dataframe 基于部分字符串匹配从另一个 dataframe 填充一个数据框列 - Based on Partial string Match fill one data frame column from another dataframe 根据另一个数据框对数据框的列进行分组 - Group a column of a data frame based on another dataframe 如何根据另一个数据框的条件创建新的数据框 - How to create a new data frame based on conditions from another data frame 根据另一个 dataframe 的条件乘以整个 dataframe 列 - Multiply an entire dataframe column based on conditions from another dataframe 寻找一种更快的方法在数据框中创建新列,其中包含来自另一列行的字典值 - Looking for a faster way to create a new column in a data frame containing a dictionary values from the rows of another column 如何在数据框中创建新列并基于另一个 dataframe 中的多个 where 子句插入数据? - How to create new column in data frame and insert data based on multiple where clauses in another dataframe? 根据来自另一个数据框的条件创建新的数据框进行循环 - Create a new Dataframe based on conditions from another Dataframe for loop 从数据框中的另一列创建一个包含最长字符串(天)的新列 - Create a new column containing the longest string, of days, from another column on the dataframe 根据列条件从另一个 dataframe 添加值的列 - Adding a column with values from another dataframe based on column conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM