0

Seeking for Help. Hi Guys i didnt code yet because i think i need some idea to access the csv and the row. so technically i want to replace the text with the id on the CSV file

import pandas as pd
df = pd.read_csv('replace.csv')
print(df)

Please kindly view the photo. so if you see there is 3 column, so i want to replace the D Column if the D Column is Equal to A Column, then replace with the ID (column B). seeking for i idea if what is the first step or guide.. thanks

Photo

In The Photo

name  | id | Replace
james | 5  | James,James,Tom
tom   | 2  | Tom,James,James
jerry | 10 | Tom,Tom,Tom

What Im Expected Result:

name  | id | Replace
james | 5  | 5,5,2
tom   | 2  | 2,5,5
jerry | 10 | 2,2,2
3
  • Hi i added sir what i want to do Commented Jan 11, 2023 at 7:52
  • Can you help me with excel or google sheet i think that a good i dea? where is the simple way to do it? please help me to acheive that on google sheet or exel Commented Jan 11, 2023 at 8:20
  • @JvdV this is the wrong formula im using =VLOOKUP(A3,$A$1:$B$3,2,FALSE) Commented Jan 11, 2023 at 8:24

2 Answers 2

1

Excel 365:

As per my comment, if it's ok to get data in a new column and with ms365, try:

enter image description here

Formula in E2:

=MAP(C2:C4,LAMBDA(x,TEXTJOIN(",",,XLOOKUP(TEXTSPLIT(x,","),A2:A4,B2:B4,"",0))))

Or, if all values will be present anyways:

=MAP(C2:C4,LAMBDA(x,TEXTJOIN(",",,VLOOKUP(TEXTSPLIT(x,","),A2:B4,2,0))))

Google-Sheets:

The Google-Sheets equivalent, as per your request, could be:

=MAP(C2:C4,LAMBDA(x,INDEX(TEXTJOIN(",",,VLOOKUP(SPLIT(x,","),A2:B4,2,0)))))

Python/Pandas:

After some trial and error I came up with:

import pandas as pd
df = pd.read_csv('replace.csv', sep=';')
df['Replace'] = df['Replace'].replace(pd.Series(dict(zip(df.name, df.id))).astype(str), regex=True)
print(df)

Prints:

    name  id Replace
0  James   5   5,5,2
1    Tom   2   2,5,5
2  Jerry  10   2,2,2

Note: I used the semi-colon as seperator in the function call to open the CSV.

Sign up to request clarification or add additional context in comments.

8 Comments

Hello thank you im getting error text slip how to fix that?
it is possible to upload the file? this is for excel right? not google sheet?
@nidiv, I also included a Pandas solution that seems to work fine too.
it is possible to avoid #N/A when the "name" is missing?
For GS: =MAP(C2:C4,LAMBDA(x,INDEX(TEXTJOIN(",",,IFERROR(VLOOKUP(SPLIT(x;","),A2:B4,2,0),"Missing"))))) I suppose. For Excel you should use the XLOOKUP() variant @nidiv
|
0

Nested =substitute functions would make this easy.

=substitute(substitute(substitute(d2, a2, b2),a3,b3),a4,b4) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.