小编cmj*_*607的帖子

根据列值将 pandas 数据框分成“块”

解释我想要完成的任务的最好方法可能只是一个例子。给定以下数据帧:

     tag  ID
0     0   1
1     0   2
2     1   3
3     1   4
4     0   5
5     1   6
6     0   7
7     0   8
8     1   9
9     1  10
10    0  11
11    0  12
12    0  13
13    1  14
14    1  15
15    1  16
16    0  17 
Run Code Online (Sandbox Code Playgroud)

根据标签,将数据帧分成“块”。当一个块被识别时,它被存储在一个单独的数据帧中(或者可能是一个数据帧列表?)。“分块”的标准是在标签列中查找 2 个或更多零。如果有超过 2 个零,则删除前一个零和当前零之间存在的所有数据。

在上面的示例数据帧中,代码将删除索引的行:0,1,6,7,10,11,12...然后它将以下块存储到单独的数据帧中:

     tag  ID
2     1   3
3     1   4
4     0   5
5     1   6

     tag  ID
8     1   9
9 …
Run Code Online (Sandbox Code Playgroud)

python pandas

5
推荐指数
1
解决办法
2443
查看次数

在 python plt.plot 中使用系列作为标记大小

是否可以使用数据框中的列来缩放 matplotlib 中的标记大小?当我执行以下操作时,我不断收到有关使用系列的错误。

import pandas as pd
import matplotlib.pyplot as plt

my_dict = {'Vx': [16,25,85,45], 'r': [1315,5135,8444,1542], 'ms': [10,50,100, 25]}
df= pd.DataFrame(my_dict)
fig, ax = plt.subplots(1, 1, figsize=(20, 10))
ax.plot(df.Vx, df.r, '.', markersize= df.ms)
Run Code Online (Sandbox Code Playgroud)

当我跑

ValueError: setting an array element with a sequence.
Run Code Online (Sandbox Code Playgroud)

我猜它不喜欢我向标记喂食一个系列的事实,但必须有办法让它工作......

python matplotlib pandas

2
推荐指数
1
解决办法
2654
查看次数

标签 统计

pandas ×2

python ×2

matplotlib ×1