Home > Software design >  Separate the given numeric values from column of the dataframe? (df[‘col1]) = [‘a_1’,’b_2’,’c_3’,’d_
Separate the given numeric values from column of the dataframe? (df[‘col1]) = [‘a_1’,’b_2’,’c_3’,’d_

Time:07-10

** task is to separate the numbers from the each element of respective row from pandas dataframe

column name is col1 and column contains elements in form of list

[‘a_1’,’b_2’,’c_3’,’d_4’] **

CodePudding user response:

IIUC, you can do this at least these two ways:

df = pd.DataFrame()
df['col1'] = ['a_1', 'b_2', 'c_3', 'd_4']
df

Input dataframe:

  col1
0  a_1
1  b_2
2  c_3
3  d_4

Option 1, using .str.split:

df['col1'].str.split('_', expand=True)

Output:

   0  1
0  a  1
1  b  2
2  c  3
3  d  4

Option 2, using .str.extract with regex:

df['col1'].str.extract('\_(\d )')

Output:

   0
0  1
1  2
2  3
3  4

CodePudding user response:

df = pd.DataFrame()
df['col1'] = [['a_1', 'b_2', 'c_3', 'd_4']]

Input:

                   col1
0  [a_1, b_2, c_3, d_4]

Doing:

df['col1'] = df['col1'].apply(lambda x: [i.split('_')[1] for i in x])

Output:

           col1
0  [1, 2, 3, 4]
  • Related