Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to create new column with different values based on matches #34

Open
JohannesWiesner opened this issue Aug 5, 2024 · 3 comments

Comments

@JohannesWiesner
Copy link
Owner

https://stackoverflow.com/questions/68869434/create-an-pandas-column-if-a-string-from-a-list-matches-from-another-column

@JohannesWiesner
Copy link
Owner Author

JohannesWiesner commented Aug 5, 2024

For example, this would create new column "file_type" that has the values "image" and "motion" depending on whether the path in the filepath column contains ".nii" or ".txt":

# add info if files is image or motion files
pattern_dict = {".nii": "image", ".txt": "motion"}
df['file_type'] = df['filepath'].apply(
    lambda path: [val for key, val in pattern_dict.items() if key in path][0])

Would be nice if one could also make this work with regexes. The following code goes already in the right direction but it will only replace the found match (not the whole string) with the value:

# Create a new column based on pattern matching
pattern_dict = {r'^.nii$': 'image', r'^.txt$': 'motion'}
df['file_type'] = df['filepath'].replace(pattern_dict, regex=True)

@JohannesWiesner
Copy link
Owner Author

This would be the corresponding regex:

^(?=.abc).

https://chatgpt.com/c/673b627e-70b8-8011-a90d-649a949fca76

But then we could also simply use the above and check if substring is in string

@JohannesWiesner
Copy link
Owner Author

This would be the corresponding code:

pattern_dict = {"^(?=.*.nii).*":"image", "^(?=.*.txt).*": "motion"}
df['file_type'] = df['filepath'].replace(pattern_dict,regex=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant