Created
February 14, 2025 21:01
-
-
Save drHyperion451/d64df0b7c794b6ccb770bc2a37b0067f to your computer and use it in GitHub Desktop.
Excel formula: Extracts any element n in a string separated by commas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
def generate_excel_formula(n): | |
""" | |
generate_excel_formula(n) --> Generates any excel formula that find an element in a substring: | |
| data | 1 | 2 | 3 | 4 | | |
|---------------------|------|--------|---------|---| | |
| dogs, cats, monkeys | dogs | cats | monkeys | | | |
| dogs, whales | dogs | whales | | | | |
| | | | | | | |
""" | |
if n == 1: | |
return '=TRIM(LEFT(A2,FIND(",",A2&",")-1))' | |
else: | |
return f'=IFERROR(TRIM(MID(A2,FIND("#",SUBSTITUTE(A2,",","#",{n-1}))+1,FIND("#",SUBSTITUTE(A2,",","#",{n}))-FIND("#",SUBSTITUTE(A2,",","#",{n-1}))-1)),"")' | |
# Ejemplo de uso | |
for i in range(1,7): | |
print(f"{generate_excel_formula(i)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment