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 · 1 comment

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

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 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)

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