首页 > 解决方案 > How to insert strings from one column of pandas DataFrame to another column at specific index?

问题描述

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)

标签: pythonstringpandasdataframe

解决方案


就如此容易:

>>> 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)

参考:
Pandas 从另一列的字符串切片创建新列
在 pandas/python 的数据框中合并两列文本


推荐阅读