【发布时间】:2019-12-03 05:34:25
【问题描述】:
我的 Pandas 数据框中存储了以下数据:
Factor SimTime RealTime SimStatus
0 Factor[0.48] SimTime[83.01] RealTime[166.95] Paused[F]
1 Factor[0.48] SimTime[83.11] RealTime[167.15] Paused[F]
2 Factor[0.49] SimTime[83.21] RealTime[167.36] Paused[F]
3 Factor[0.48] SimTime[83.31] RealTime[167.57] Paused[F]
我想创建一个只包含 [] 中所有内容的新数据框。
我正在尝试使用以下代码:
df = dataframe.apply(lambda x: x.str.slice(start=x.str.find('[')+1, stop=x.str.find(']')))
但是,我在df 中看到的只是 NaN。为什么?这是怎么回事?我应该怎么做才能达到预期的行为?
【问题讨论】:
-
如果有人解决了,请告诉我为什么这种方法是错误的以及如何解决这个问题?
-
@abhilb 提供了解决方案。检查下面。
-
没关系,但我想知道这种方法有什么问题,我只是不想通过查看另一种方法来忽略这个问题。谢谢。
-
你说得对,不过,这只是我提取数据的工具,所以我暂时对分辨率没问题。不过,如果有人看到它并在这里指出任何错误,我会留意这里。
标签: python python-3.x pandas dataframe data-analysis