我試圖在循環(huán)遍歷 pandas 數(shù)據(jù)框的列時訪問下一列標(biāo)題。for cols in data.columns:
if data.columns.get_loc(cols) < len(data.columns): # skip last column of data
count = data.groupby([cols, cols+1]).size() # create new df and how many times the two columns occur但是cols+1給我一個錯誤。這是因為cols返回標(biāo)題名稱,所以你不能 +1 一個字符串,但是在這樣的循環(huán)中獲取下一列標(biāo)題的最佳方法是什么?
2 回答

慕田峪4524236
TA貢獻(xiàn)1875條經(jīng)驗 獲得超5個贊
您可以枚舉列
for col_index, cols in enumerate(data.columns): if col_index+1 < len(data.columns): # skip last column of data count = data.groupby([cols,data.columns[col_index+1]]).size() # create new df and how many times the two columns occur

長風(fēng)秋雁
TA貢獻(xiàn)1757條經(jīng)驗 獲得超7個贊
for col_idx in range(len(data.columns)-1): if data.columns.get_loc(data.columns[col_idx]) < len(data.columns): count = data.groupby([col_idx, col_idx+1]).size()
添加回答
舉報
0/150
提交
取消