python - 如何用 pandas reshape 数据框

标签 python pandas

我有一个如下图所示的数据框,如何有效地获取单元格中带有“:”的所有值并创建一个新的数据框?例如,“cnt:1”应转换为“1”; “ack:dsn:113”应转换为“113”等。 enter image description here

最佳答案

您可以使用rsplit限制为 1 并选择第二个值 list s:

df = df.applymap(lambda x: x.rsplit(':', 1)[1])

或者:

df = df.apply(lambda x: x.str.rsplit(':', 1).str[1])

print (df)
   dsn cnt retry rssir lqir rssif lqif
0  113   1     1   -24    6   -49    5
1  114   2     1   -24   10   -49   15
2  115   3     1   -24    5   -59   14
3  116   4     1   -24    8   -58   11
4  117   5     1   -24   12   -57   14

或者更简单地指出Anton vBR :

df = df.applymap(lambda x: x.rsplit(':')[-1])

df = df.apply(lambda x: x.str.rsplit(':').str[-1])

关于python - 如何用 pandas reshape 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50480760/

相关文章:

python - Scapy 原始套接字

python - 有没有办法在 bigquery 中使用 kmeans、tensorflow 保存的模型?

python - 在python中生成二进制数的二维数组

python - 索引下降级别

python - 将 x=y 线添加到包含箱线图的绘图中

Python:如何并行运行两个函数

python - 在 Python 中为 os.path.expandpath 使用自定义环境变量

python绘图通过groupby过滤

python - 在数据框中设置样式后执行操作

python - 在 pandas 中绘制 timedelta 与时间