我的问题
我们将此数组类型数据作为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)
第二列是每一行的唯一IP。它的类型是str。
最后一列是每一行的一堆分布。它的类型是str。
我想将唯一IP附加到最后一列。
我的尝试
我尝试使用以下代码:
V = []
([V.append(m[3][1]), V.append(m[1][1])])
但是,这导致了错误的输出:
['loglaplace(c=1.88, loc=-932.82, scale=674382.82)',
'IP:slaL5jw']
尽管我可以使用str( ['loglaplace(c=1.88, loc=-932.82, scale=674382.82)',
'IP:slaL5jw'])
所需输出示例:
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)
答案 0 :(得分:2)
就这么简单:
>>> 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 make new column from string slice of another column
Combine two columns of text in dataframe in pandas/python
答案 1 :(得分:1)
像这样吗?
df[3].combine(df[1], lambda x, y: x.replace(")", ", {})".format(y)))
pandas.Series.combine
是一个很好的函数,您可以使用它一次将转换应用于两个列,以产生第三列或替换其中的一个。在这种情况下,它只是将列3中的尾随")"
替换为列1中的值。