2

My problem

We have this array type data as m:

    0   1   2   3
0   746200.0    IP:aWSrjjB  foldcauchy  foldcauchy(c=3.40, loc=853.32, scale=188436.01)
1   1061881.5   IP:joW6uH4  johnsonsu   johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41)
2   645000.0    IP:4Q3L2kB  foldcauchy  foldcauchy(c=3.94, loc=835.77, scale=184545.16)
3   284375.0    IP:WLP1cdn  loglaplace  loglaplace(c=1.81, loc=-1001.33, scale=701001.33)
4   666600.0    IP:kQn348T  johnsonsu   johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41)
5   754678.5    IP:kQn348T  loglaplace  loglaplace(c=1.93, loc=-1087.33, scale=786087.33)

The second column is a unique IP for each row. Its type is str.

The last column is a bunch of distributions for each row. its type is str.

I would like to append the unique IP to the last column.


My attempt

I tried using the following code:

V = []
([V.append(m[3][1]), V.append(m[1][1])])

Yet, that resulted in the wrong output:

 ['loglaplace(c=1.88, loc=-932.82, scale=674382.82)',
 'IP:slaL5jw']

Although I could use str( ['loglaplace(c=1.88, loc=-932.82, scale=674382.82)', 'IP:slaL5jw'])


Example of the desired output:

   0   1   2   3
0   746200.0    IP:aWSrjjB  foldcauchy  foldcauchy(c=3.40, loc=853.32, scale=188436.01, IP:aWSrjjB)
1   1061881.5   IP:joW6uH4  johnsonsu   johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41, IP:joW6uH4)
Georgy
  • 12,464
  • 7
  • 65
  • 73
Abdulaziz Al Jumaia
  • 436
  • 2
  • 5
  • 22

2 Answers2

3

As simple as that:

>>> df[3] = df[3].str[:-1] + ', ' + df['1'] + ')'
>>> df
    0   1   2   3
0   746200.0    IP:aWSrjjB  foldcauchy  foldcauchy(c=3.40, loc=853.32, scale=188436.01, IP:aWSrjjB)
1   1061881.5   IP:joW6uH4  johnsonsu   johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41, IP:joW6uH4)
2   645000.0    IP:4Q3L2kB  foldcauchy  foldcauchy(c=3.94, loc=835.77, scale=184545.16, IP:4Q3L2kB)
3   284375.0    IP:WLP1cdn  loglaplace  loglaplace(c=1.81, loc=-1001.33, scale=701001.33, IP:WLP1cdn)
4   666600.0    IP:kQn348T  johnsonsu   johnsonsu(a=-0.39, b=0.46, loc=715076.10, scale=70401.41, IP:kQn348T)
5   754678.5    IP:kQn348T  loglaplace  loglaplace(c=1.93, loc=-1087.33, scale=786087.33, IP:kQn348T)

References:
Pandas make new column from string slice of another column
Combine two columns of text in dataframe in pandas/python

Georgy
  • 12,464
  • 7
  • 65
  • 73
1

Something like this?

df[3].combine(df[1], lambda x, y: x.replace(")", ", {})".format(y)))

pandas.Series.combine is a nice function you can use to apply a transformation to two columns at once producing a third or replacing one of them. In this case it's just replacing the trailing ")" in your column 3 with the value from column 1.

Georgy
  • 12,464
  • 7
  • 65
  • 73
filippo
  • 5,197
  • 2
  • 21
  • 44