id
1
2
1
2
3
1
2
3
4
有一列.csv表格,本人想用回圈陳述句生成新的表格如下:
id gr
1 1
2 1
1 2
2 2
3 2
1 3
2 3
3 3
4 3
有點像打組的感覺,有大神幫忙解決嗎?
我自己寫的代碼如下:
import pandas as pd
data = pd.read_csv('C:/Users/lenovo/Desktop/01.csv', engine='python', encoding='unicode_escape')
a = 1
num = 0 # 計數器,也就是b列要填的值
for i in data['id']: # 假設第一列的陣列叫aarray
if a == 1:
num += 1
data.loc[i, 'gr'] = num # 假設第二列的陣列叫barray
else:
data.loc[i, 'gr'] = data.loc[i, 'gr']
這個代碼不能實作!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/245847.html
標籤:其他
上一篇:計算機組成原理期末知識點復習
