我有一个包含汽车和特定区域速度的数据框。
\n我想转换数据帧,这样我就知道每个区域哪辆车的速度大于 100,因此我为每辆车获取一行和两列,根据 100 值区分区域。
\n输入:
\n| 车号 | \xc2\xa0speed_t1 | \xc2\xa0speed_t2 | 速度_t3 | 速度_t4 |
|---|---|---|---|---|
| 1. | 90 | 80 | 120 | 34 |
| 2. | 110 | 130 | 140 | 99 |
| 3. | 40 | 110 | 20 | 110 |
预期输出:
\n| 车号 | 正常速度 | 高速_t |
|---|---|---|
| 1. | 速度_t1,速度_t2,速度_t4 | 速度_t3 |
| 2. | 速度_t4 | 速度_t1,速度_t2,速度_t3 |
| 3. | 速度_t1,速度_t3 | 速度_t2,速度_t4 |
用于DataFrame.dot列名称带有分隔符的非迭代解决方案,因此有必要,通过以下方式删除最后一个值Series.str.strip:
df.assign(normal_speed_t=(df<100).dot(df.columns + ',').str.strip(','),
high_speed_t=(df>=100).dot(df.columns + ',').str.strip(','))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
220 次 |
| 最近记录: |