给dataframe添加一列索引

时间:2020-12-05 10:28:30

测试数据自己瞎编的

需求:给现在df数据添加一列sid,要求这一列是和stock一一对应的整数

代码如下:

import pandas as pd

test_data = {'stock': ['AAPL', 'GOOG', 'AMZN', 'AAPL', 'GOOG', 'AMZN'],
             'open': [100, 110, 120, 105, 115, 125],
             'high': [110, 120, 130, 115, 125, 135],
             'low': [90, 100, 110, 95, 105, 115],
             'close': [103, 113, 123, 108, 118, 128]}
df = pd.DataFrame(test_data)
# print(df)
grops_dic = df.groupby('stock').groups
map_dic = {}
i = 1
for key in grops_dic:
    map_dic[key] = i
    i += 1
# print(map_dic)
sid_list = []
for stock in df['stock']:
    sid_list.append(map_dic[stock])
df['sid'] = sid_list
print(df)