python - 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)
解决方案
就如此容易:
>>> 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)
推荐阅读
- image - MATLAB:如何根据另一个矩阵重新标记一个矩阵?
- apache-kafka - 在kafka中创建新主题时的决定方式是什么?
- javascript - 如何在完成发送的第一个请求之前阻止 JavaScript 发送多个 AJAX 请求
- mongodb - MongoDB并发读写对
- javascript - 递归过滤大型javascript对象
- c# - 构建 Xamarin.android 时出现问题
- visual-studio - 在 Visual Studio 2019 IntelliCode 建议中设置方法顺序?
- scala - 错误 SparkContext:初始化 SparkContext 时出错。错误实用程序:线程主线程中未捕获的异常
- c++ - 我们如何在 #define 预处理器中使用嵌套 if else
- prometheus - 如何使用 Promtail / Loki - AlertManager 为我的日志中的每个错误发送警报?