Skip to content Skip to sidebar Skip to footer

Call Specific Columns With Regular Expression Pandas

Using pandas, I want to do something like this while looping through data frames: for body_part, columns in zip(self.body_parts, usecols_gen()): body_part_df = self.rea

Solution 1:

You cannot query a DataFrame column using a regular expression, what you can do instead is iterate over it and apply your function on the matching columns, i.e.:

import re

    # ...

    for body_part, columns in zip(self.body_parts, usecols_gen()):
        body_part_df = self.read_csv(usecols=columns)
        if self.normalize:
            for column in body_part_df:
                if re.match(r"x(\.\d)?", column):  # or re.search() for partial matches
                    body_part_df[column] = body_part_df[column].apply(lambda x: x/x_max)
        print(body_part_df)
        result[body_part] = body_part_df

Post a Comment for "Call Specific Columns With Regular Expression Pandas"