[英]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:将根据以下条件选择列:
if word 'COORDINATES' is in the column如果单词 'COORDINATES' 在列中
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.