0

Iam trying to compile some of the columns in df1 to a new dataframe df2.

the columns will be selected based on the following conditions:

  1. if word 'COORDINATES' is in the column

  2. if word 'ID' is in the column

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)

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)

much appreciated your help on this.

Original Table (df1)

enter image description here

Required Output(df2)

enter image description here

1 Answer 1

1

I think this should work

cols = [col for col in df1.columns if  'Coordinates' in col or 'ID' in col]
Sign up to request clarification or add additional context in comments.

2 Comments

i tried that, it takes the whole df. any other suggestions?
are you sure, I just tried it on my computer using the same column names as you have and it worked fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.