0

I have this multiple datasets that are needed to be processed by pandas.

DF 1:
DATE DATA1_ID DATA
20200101 XXX 123
20200102 YYY 456
...

DF 2:
DATE DATA2_ID DATA
20200101 AAA 098
20200102 BBB 765
...

What I want to achieve is to have a generic filter for those two tables like using df.loc[df.DATA1_ID == "XXX"] without having to change the columns names on either of the data frames.

I want to search this "ID" basing from the IDs columns that will work on both dataframes.

Sample behavior when running the script on both data frame:

  1. Filtering ID = XXX. Results would be DF 1: 20200101 XXX 123. DF 2:
  2. Filtering ID = AAA. Results would be DF 1: . DF 2: 20200101 AAA 098
  3. Filtering ID = OOO. Results would be DF 1: . DF 2:
2
  • Does the position of the ID column change? Or is it always the second column over in the dataframes? Commented Aug 27, 2020 at 17:28
  • Sorry for the late response. But yes. the ID column position changes between dataframes. Commented Sep 1, 2020 at 6:09

1 Answer 1

1

I can think of two options:

  1. rename the columns of interest to one unique name. Depending on the number of dataframes and meaning of column the columns' names, this may be too clumsy.
  2. If the columns you're wishing to access are all on the same position in the dfs, you could access the nth column name in the columns' list. Something like df.columns[1].
  3. You can find the id column:
id_cols = [col for col in df.columns if 'id' in col]
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, sorry for the extremely late response. (1) there is no build-in module in pandas that can achieve what I want? (2) The column of the IDs can is not constant and can change without prior notice.
@Dense04 I added an option that finds the ID column, supposing the id column is the one containing 'id' in its name. Depending on the possible values of the column names, you may need to refine the filter, but you get the idea.

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.