Home > Blockchain >  find out if the indexes of a grouped data frame match a column of another dataframe?
find out if the indexes of a grouped data frame match a column of another dataframe?

Time:08-04

I have a grouped data frame named df_grouped where AF & Local are the indexes. I would like to assert whether the indexes in df_grouped are equal to a column from another dataframe df[A].

This is an example of my code

import pandas as pd

data = {'Number': [5678, 2934],
        'Age': [93, 88],}
df_grouped= pd.DataFrame(data, index=["AF","Local"])

data2 = {"A":["AF","Local"],
        "Location":["US","China"]}
df=pd.DataFrame(data2)

I tried this but it does not work:

assert df["A"].isin(df_grouped.index)

CodePudding user response:

To use assert for pandas series you can use assert_series_equal which checks that left and right Series are equal.

from pandas import testing as tm
tm.assert_series_equal(df["A"], df_grouped.index.to_series(index=df["A"].index, name= df["A"].name))

which will give you error if series values are different.

AssertionError: Series.index are different

  • Related