简体   繁体   English

当当前行和先前行中的名称(A 列中)匹配时,如何创建具有先前值(B 列中)的列?

[英]How do I create a column with a previous value (in column B) when the names (in column A) in current and previous rows matches?

I want to create a new column that copies the values in column "value" corresponding to the same person in column "Name" but from the immediate previous row with the same name.我想创建一个新列,复制“值”列中的值,该值对应于“名称”列中的同一个人,但来自具有相同名称的前一行。 I want to leave an empty string when there are not previous values for that person.当那个人没有以前的值时,我想留下一个空字符串。

I tried to use this code, but it doesn't work:我尝试使用此代码,但它不起作用:

previous_value= []
col_list = df['Name'].values.tolist()

for idx in df.index:
    last_name= df['Name'].loc[idx] 
    last_value= df['Value'].loc[idx]
    
    for i in range(len(col_list)-1):
        actual_name= col_list[i+1]
        if last_name == actual_name:
            previous_value.append(last_value)
        else:
            previous_followers.append("")

My idea was to transform later the previous_value list into a data frame and then add it to the original data frame.我的想法是稍后将 previous_value 列表转换为数据框,然后将其添加到原始数据框。

This is how it should look like:它应该是这样的:


        Name    Value   Previous_value
1      Andrew   12         
2      Marco    10          
3      Philips   9 
4      Andrew    8           12
5      Oscar     7     
6      Peter    15    
7      Maria    25
8      Marco     3           10
9      Andrew    7           8
10     Oscar    19           7
11     Oscar    21          19
12     Maria     2          25

Thank you谢谢

This question was answered previously here .这个问题之前在这里得到了回答。 You can use groupby and shift to achieve this (although by default you will get NaNs for the first entry, not an empty string.您可以使用groupbyshift来实现此目的(尽管默认情况下您将获得第一个条目的 NaN,而不是空字符串。

df = pd.DataFrame({'Name':[1,2,3,1,2,3,1,2,3],'Value':[0,1,2,3,4,5,6,7,8]})
df['Previous_Value'] = df.groupby('Name')['Value'].shift()

For loops often don't mix well with pandas. In this case, you want to group by name and then shift the values down by one to create the previous value column. For 循环通常不能很好地与 pandas 混合使用。在这种情况下,您希望按名称分组,然后将值向下移动一个以创建先前的值列。 This should do the trick:这应该可以解决问题:

>>> df['previous_value'] = df.groupby('Name')['Value'].shift()
>>> df
       Name  Value  previous_value
0    Andrew     12             NaN
1     Marco     10             NaN
2   Philips      9             NaN
3    Andrew      8            12.0
4     Oscar      7             NaN
5     Peter     15             NaN
6     Maria     25             NaN
7     Marco      3            10.0
8    Andrew      9             8.0
9     Oscar     19             7.0
10    Oscar     21            19.0
11    Maria      2            25.0

You can then use fillna('') on the new column to replace the NaNs with an empty string if desired.然后,如果需要,您可以在新列上使用fillna('')将 NaN 替换为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从当前列中减去前一列,并使用numpy使用该值在数组中创建新维? - How to subtract the previous rows column from current column and create a new dimension in the array with this value using numpy? 具有先前行值的新列 - New column with previous rows value Pandas:如何使用现有列和新创建列中的先前行创建新列? - Pandas : How can I create new column using previous rows from existing column and newly created column? 选择当前行中列值为 1 但上一行中值为 0 的行 - Selecting rows where column value is 1 in the current row, but 0 in the previous row 我想基于上一列添加一个新的 DataFrame 列,以便如果上一列元素与列表值匹配,则更改该值 - I want to add a new DataFrame column based on previous column such that if previous column element matches with list value, change the value 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row 将上一列值与当前值相加 - Add the previous column value with the current value 熊猫用以前的值填充空列名 - Pandas fill empty column names with previous value 创建一个前N行的新列作为数组 - Create a new Column of previous N rows as an Array 使用前面的行创建一个新列,pandas - Create a new column using the previous rows , pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM