Home > front end >  How to check if positional indexes in a list exists in section of corresponding DataFrame
How to check if positional indexes in a list exists in section of corresponding DataFrame

Time:04-25

I have a DataFrame like this:

Date          A      B      C
2021-08-20    1      2      3
2021-08-21    2      3      4
2021-08-22    3      4      5
2021-08-23    4      5      6
2021-08-24    7      8      9
2021-08-25    10     11     12
2021-08-26    11     12     13
2021-08-28    12     13     14

My "target" section is dates from 2021-08-21 to 2021-08-24.

Now I have a list of positional indices:

A = [0, 1, 3, 4, 6, 7]

What I'm trying to do is create a new list of indices that correspond to the indices only in my target section, and then find the total number of elements in the new list.

Target answer:

new_list = [1, 3, 4]
print(len(new_list))
3

I've tried this so far:

new_list = []
df_range = df.loc['2021-08-21':'2021-08-24']

for data_idx in A:
    if data_idx == df_range.iloc[data_idx]:
        new_list.append(data_idx)
print(len(new_list))

But I get IndexErrors (single positional indexer is out-of-bounds) or Key errors (for a similar attempt). I believe what's erroring is when the program tries to locate the indexes outside of this range?

Thank you in advance and sorry if anything is confusing. I know there should be an easy way to do this but I just can't figure it out.

CodePudding user response:

If I understood the question, you're wanting to have a list with corresponding indexes to your df_range? If so these two approaches are commonly used for that

new_list = []
df_range = df.loc['2021-08-21':'2021-08-24']

for i, v in enumerate(df_range):
    new_list.append(i)
    
    
for i in range(len(df_range)):
    new_list.append(i)

CodePudding user response:

IIUC:

A = [0, 1, 3, 4, 6, 7]

df["tmp"] = range(len(df))
x = df.loc["2021-08-21":"2021-08-24"]
print(x.loc[x["tmp"].isin(A), "tmp"].to_list())

Prints:

[1, 3, 4]
  • Related