使用 if 循环遍历一个系列,将数据分类到不同的桶中
Iterating through a series using an if loop to sort data into different buckets
我目前有一个包含一列的数据框:
import pandas as pd
data = {'Time': [55.760, 140.685, 53.543, 53.521, 219.675, 54.095, 58.000, 134.258, 52.860, 54.852, 147.068, 49.160, 53.244]
}
c = pd.DataFrame(data)
index Time
0 55.760
2 140.685
4 53.543
6 53.521
8 219.675
10 54.094
12 58.000
14 134.257
16 52.860
18 54.852
20 147.067
22 49.160
24 53.244
if c in range(0,25):
ex = .2
elif c in range(25,60):
ex = .4
else:
ex = .6
我需要查看每个值,如果它是从 0 < 25,ex =.2 等等,就像在 if 循环中一样。
然后将 ex 值用于 运行 之后变化的等式中。现在如何设置我收到一条错误消息:
ValueError:DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
我现在不太确定要更改什么。
使用 np.select
代替 if
语句:
np.select([c < 25, (c >= 25) & (c < 60), c >= 60], [.2, .4, .6])
我目前有一个包含一列的数据框:
import pandas as pd
data = {'Time': [55.760, 140.685, 53.543, 53.521, 219.675, 54.095, 58.000, 134.258, 52.860, 54.852, 147.068, 49.160, 53.244]
}
c = pd.DataFrame(data)
index Time
0 55.760
2 140.685
4 53.543
6 53.521
8 219.675
10 54.094
12 58.000
14 134.257
16 52.860
18 54.852
20 147.067
22 49.160
24 53.244
if c in range(0,25):
ex = .2
elif c in range(25,60):
ex = .4
else:
ex = .6
我需要查看每个值,如果它是从 0 < 25,ex =.2 等等,就像在 if 循环中一样。 然后将 ex 值用于 运行 之后变化的等式中。现在如何设置我收到一条错误消息:
ValueError:DataFrame 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。 我现在不太确定要更改什么。
使用 np.select
代替 if
语句:
np.select([c < 25, (c >= 25) & (c < 60), c >= 60], [.2, .4, .6])