How can I get value in specific position in list form of dataframe?

Question:

NOTE:

Here is the reproducable expamle:

import pandas as pd
import numpy as np
df = pd.DataFrame({'index':[21144,64182,64184,64197,64198],'농약':['5. 방법/수단 : 농약_바스타_글루포시네이트암모늄' ,'5. 방법/수단 : 농약_바스타_글루포시네이트암모늄','5. 방법/수단 : 농약_확타_글루포시네이트암모늄' ,'5. 방법/수단 : 농약_모름_글루포시네이트','5. 방법/수단 : 농약_바로바로_글루포시네이트']})
df.set_index('index',inplace=True,append=False)
pes_df = df['농약'].str.split('_',expand=False)
pes_df

I have a list form of column in pandas dataframe. Here is the sample dataset

21144                                                                                                      [5. 방법/수단 : 농약, 바스타, 글루포시네이트암모늄 ]
64182                                                                                                      [5. 방법/수단 : 농약, 바스타, 글루포시네이트암모늄 ]
64184                                                                                                       [5. 방법/수단 : 농약, 확타, 글루포시네이트암모늄 ]
64197                                                                                                          [5. 방법/수단 : 농약, 모름, 글루포시네이트 ]
64198                                                                                                        [5. 방법/수단 : 농약, 바로바로, 글루포시네이트 ]
64200                                                                                                   [5. 방법/수단 : 농약, 레드샷, 글리포세이트이소프로필아민 ]
64205                                                                                                             [5. 방법/수단 : 농약, 모름, 비펜트린 ]
64212                                                                                                           [5. 방법/수단 : 농약, 그라목손, 파라콰트 ]
64219                                                                                                          [5. 방법/수단 : 농약, 판데스, 사이퍼메트린 ]
64253                                                                                                [5. 방법/수단 : 농약, 테라도플러스, 글리포세이트이소프로필아민 ]
64254                                                                                                   [5. 방법/수단 : 농약, 근사미, 글리포세이트이소프로필아민 ]
64294                                                                                                               [5. 방법/수단 : 농약, 모름, 모름 ]
64295                                                                                                   [5. 방법/수단 : 농약, 푸레쎈, 글리포세이트이소프로필아민 ]
64301                                                                                                         [5. 방법/수단 : 농약, 시반토, 플루피라디퓨론 ]
64336                                                                                                   [5. 방법/수단 : 농약, 푸레쎈, 글리포세이트이소프로필아민 ]
64350                                                                                           [5. 방법/수단 : 농약, 농약, 인바이오글라신, 글리포세이트이소프로필아민 ]

I want to manipulate the second column of this list. For instance, replacing 모름 -> 999

the output would be like this

21144                                                                                                      [5. 방법/수단 : 농약, 바스타, 글루포시네이트암모늄 ]
64182                                                                                                      [5. 방법/수단 : 농약, 바스타, 글루포시네이트암모늄 ]
64184                                                                                                       [5. 방법/수단 : 농약, 확타, 글루포시네이트암모늄 ]
64197                                                                                                          [5. 방법/수단 : 농약, 999, 글루포시네이트 ]
64198                                                                                                        [5. 방법/수단 : 농약, 바로바로, 글루포시네이트 ]
64200                                                                                                   [5. 방법/수단 : 농약, 레드샷, 글리포세이트이소프로필아민 ]
64205                                                                                                             [5. 방법/수단 : 농약, 999, 비펜트린 ]
64212                                                                                                           [5. 방법/수단 : 농약, 그라목손, 파라콰트 ]
64219                                                                                                          [5. 방법/수단 : 농약, 판데스, 사이퍼메트린 ]
64253                                                                                                [5. 방법/수단 : 농약, 테라도플러스, 글리포세이트이소프로필아민 ]
64254                                                                                                   [5. 방법/수단 : 농약, 근사미, 글리포세이트이소프로필아민 ]
64294                                                                                                               [5. 방법/수단 : 농약, 999, 모름 ]
64295                                                                                                   [5. 방법/수단 : 농약, 푸레쎈, 글리포세이트이소프로필아민 ]
64301                                                                                                         [5. 방법/수단 : 농약, 시반토, 플루피라디퓨론 ]
64336                                                                                                   [5. 방법/수단 : 농약, 푸레쎈, 글리포세이트이소프로필아민 ]
64350                                                                                           [5. 방법/수단 : 농약, 농약, 인바이오글라신, 글리포세이트이소프로필아민 ]

and I would like to join it with special letter ‘_’.

so the outcome would be like this for the index 21144

21144                                                                                                      5. 방법/수단 : 농약_바스타_글루포시네이트암모늄 
Asked By: Chung Joshua

||

Answers:

Example

i make minimal and reproducible example

data = ['method_Basta_glufosinate', 'method_unknown_ammonium', 'method_unknown_ammonium']
df = pd.Series(data).to_frame('col1')

df

    col1
0   method_Basta_glufosinate
1   method_unknown_ammonium
2   method_unknown_ammonium

Code

unknown -> 999

you don need make list for changing your unknown to 999

use str.replace

df['col1'].str.replace('unknown', '999')

result:

0    method_Basta_glufosinate
1         method_999_ammonium
2         method_999_ammonium
Name: col1, dtype: object

Are there any additional reasons for needing list? then use following code:

df['col1'].apply(lambda x: [i if i != 'unknown' else 999 for i in x.split('_')])

output:

0    [method, Basta, glufosinate]
1         [method, 999, ammonium]
2         [method, 999, ammonium]
Name: col1, dtype: object
Answered By: Panda Kim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.