我想结合使用pandas.cut()
函数和定义的时间间隔,以这些时间间隔对给定的数据进行排序。我还想给这些间隔名称起一个小,中等和高的名称。我尝试使用以下代码进行此操作:
import pandas as pd
CO_simplified = pd.IntervalIndex.from_tuples([(0, 200), (200,250 ), (300, 1000)]) #small,moderate,high
df_dtc_test= pd.DataFrame()
df_dtc_test["CO_simp"] = pd.cut([122,232,333,324,533], len(CO_simplified), labels=CO_simplified)
print(df_dtc_test)
有输出:
CO_simp
0 (0, 200]
1 (0, 200]
2 (200, 250]
3 (200, 250]
4 (300, 1000]
但是这不是我所期望的,第一个索引号对我来说似乎是正确的,但是第二个索引号也按组(0,200)进行排序,但是第二个索引的给定值是232,在此间隔之外。除了错误的排序,我想用“ small”替换例如(0,200)。
编辑:我的问题已部分解决(请参阅下文),我唯一关心的是如何用名称替换间隔。
有人知道我该怎么做吗?
以正确的方式订购:
使用
df_dtc_test["CO_simp"] = pd.cut([122,232,333,324,533], CO_simplified, labels=CO_simplified)
代替
df_dtc_test["CO_simp"] = pd.cut([122,232,333,324,533], len(CO_simplified), labels=CO_simplified)