使用相应的过去索引值和特定的唯一列值填充新的数据帧列

2024-06-06 11:25:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有一个优雅的方式做,我做了很长时间,我相信非常粗糙的方式。假设我们有一个数据帧,其中有两列:“col1”和“col2”。行数为13“Col1”包含三个变量:“a”、“b”和“c”col2'包含随机数值。现在,我想创建一个名为“teststat”的新列,它包含“col2”中包含的值,该值在“col1”中的变量是上次出现时的值,如果这是第一次出现,则等于当前值。例如,如果“a”出现在第0、1、4、6和12行,并且在这些索引位置的col2值是32、432、56、4和34,那么这些位置的test stat值应该是32、32、432、56和4。你知道吗

我想要的示例数据集:

index   col1    teststat    col2
  0      a         32.0       32
  1      a         32.0      432
  2      b        433.0      433
  3      c          4.0        4
  4      a        432.0       56
  5      c          4.0       64
  6      a         56.0        4
  7      b        433.0      535
  8      c         64.0      643
  9      c        643.0      356
 10      b        535.0       32
 11      b         32.0      535
 12      a          4.0       34

我使用了下面的代码,它使用的逻辑是存储“a”、“b”、“c”中的特定值的索引,然后用for循环编写单独的代码,但是我可以看到,在放大时,这可能会成为一个问题,例如,如果“col1”中只有3个唯一值,而不是500多个单独的唯一值。我想要一个解决方案/逻辑,说明可以为该场景做些什么。我添加了以下代码:

单元格[1]

for vals in list(df['col1'].unique()):
    if vals=='a':
        idxa = df.index[df['col1']=='a']
    if vals=='b':
        idxb = df.index[df['col1']=='b']
    if vals=='c':
        idxc = df.index[df['col1']=='c']    

单元格[2]

for i in range(len(idxa)):
    if i==0:
        df.loc[idxa[i],'test_stat']=df.loc[idxa[i],'col2']
    else:
        df.loc[idxa[i],'test_stat']=df.loc[idxa[i-1],'col2']


for i in range(len(idxb)):
    if i==0:
        df.loc[idxb[i],'test_stat']=df.loc[idxb[i],'col2']
else:
    df.loc[idxb[i],'test_stat']=df.loc[idxb[i-1],'col2']

for i in range(len(idxc)):
    if i==0:
        df.loc[idxc[i],'test_stat']=df.loc[idxc[i],'col2']
    else:
        df.loc[idxc[i],'test_stat']=df.loc[idxc[i-1],'col2']        

有没有更优雅/更好的方法?任何想法/帮助都将不胜感激。你知道吗


Tags: 代码intestdfforindexifloc
1条回答
网友
1楼 · 发布于 2024-06-06 11:25:52

一种方法是将groupbyshift一起使用。你知道吗

df['teststat'] = df.groupby('col1')['col2'].shift(1).fillna(df['col2'])

print(df[['col1', 'teststat', 'col2']])

    col1    teststat    col2
0      a        32.0      32
1      a        32.0     432
2      b       433.0     433
3      c         4.0       4
4      a       432.0      56
5      c         4.0      64
6      a        56.0       4
7      b       433.0     535
8      c        64.0     643
9      c       643.0     356
10     b       535.0      32
11     b        32.0     535
12     a         4.0      34

编辑

对于您的附加问题:

Let's say, i want another column 'teststat2' which gives the difference between last 2 values for a particular value in 'col1'.

你可以做以下的事情。你知道吗

df['teststat2'] = df['col2'] - df['teststat']
df.loc[df['teststat2'] == 0, 'teststat2'] = df['col2']
print(df)

    col1    teststat    col2    teststat2
0      a        32.0      32         32.0
1      a        32.0     432        400.0
2      b       433.0     433        433.0
3      c         4.0       4          4.0
4      a       432.0      56       -376.0
5      c         4.0      64         60.0
6      a        56.0       4        -52.0
7      b       433.0     535        102.0
8      c        64.0     643        579.0
9      c       643.0     356       -287.0
10     b       535.0      32       -503.0
11     b        32.0     535        503.0
12     a         4.0      34         30.0

相关问题 更多 >