Python:在pandas dataframe中基于索引重新启动添加观察

irlmq6kh  于 2023-04-10  发布在  Python
关注(0)|答案(3)|浏览(112)

我有一个dataframe,看起来像这样:

df = pd.DataFrame({'index': [ 1, 2, 3, 4, 5, 6, 1, 2, 3, 4,5,6 ],
         'data': [  1.5, 0.22, 0.323, 4.4, 5.62, 0.56, 1.32, 2.1, 3.09, 4,5.3,0.6]})

当索引计数从1重新开始时,我希望有一个新的月份。月份分配不遵循模式,但我创建了一个列表,该列表存储了我希望归因于每次重新开始计数的月份。
我想要的是这样的dataframe:

months =["April", "June"]

df = pd.DataFrame({'index': [ 1, 2, 3, 4, 5, 6, 1, 2, 3, 4,5,6 ], 
          'data': [  1.5, 0.22, 0.323, 4.4, 5.62, 0.56, 1.32, 2.1, 3.09, 4,5.3,0.6],
         'Month': ['April', 'April', 'April', 'April', 'April', 'April', 'June', 'June', 'June', 'June', 'June', 'June'] })
bvhaajcl

bvhaajcl1#

你可以遍历索引列:

import pandas as pd

df = pd.DataFrame({'index': [ 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6 ],
                   'data': [ 1.5, 0.22, 0.323, 4.4, 5.62, 0.56, 1.32, 2.1, 3.09, 4, 5.3, 0.6]})

months = ["April", "June"]
month_index = 0
month_dict = {}

for i in range(len(df)):
    if df.loc[i, 'index'] == 1:
        month_index = (month_index + 1) % len(months)
        month_dict[1] = months[month_index]
    month_dict[df.loc[i, 'index']] = months[month_index]

df['Month'] = df['index'].map(month_dict)

print(df)
lb3vh1jj

lb3vh1jj2#

让我们指定index == 1的月份,然后向前填充剩余的行

m = df['index'] == 1
df.loc[m, 'month'] = months[:m.sum()] 
df['month'] = df['month'].ffill()

结果

index   data  month
0       1  1.500  April
1       2  0.220  April
2       3  0.323  April
3       4  4.400  April
4       5  5.620  April
5       6  0.560  April
6       1  1.320   June
7       2  2.100   June
8       3  3.090   June
9       4  4.000   June
10      5  5.300   June
11      6  0.600   June
w8ntj3qf

w8ntj3qf3#

使用numpy索引:

df['Month'] = np.array(months)[df['index'].eq(1).cumsum()-1]

更安全的pandas替代方案,如果您可能有缺失值:

df['Month'] = pd.Series(months).reindex(df['index'].eq(1).cumsum()-1).values

输出:

index   data  Month
0       1  1.500  April
1       2  0.220  April
2       3  0.323  April
3       4  4.400  April
4       5  5.620  April
5       6  0.560  April
6       1  1.320   June
7       2  2.100   June
8       3  3.090   June
9       4  4.000   June
10      5  5.300   June
11      6  0.600   June

相关问题