[英]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.您可以使用
groupby
和shift
来实现此目的(尽管默认情况下您将获得第一个条目的 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.