我有一个for-while循环组合来检查人年观测值的差异。整个过程为我提供了一个布尔列表,作为结果,我需要对其进行进一步分析。
我尝试了append
的多个版本,但没有一个起作用。
这是我的数据:
import pandas as pd
df = pd.DataFrame({'year': ['2001', '2004', '2005', '2006', '2007', '2008', '2009',
'2003', '2004', '2005', '2006', '2007', '2008', '2009',
'2003', '2004', '2005', '2006', '2007', '2008', '2009'],
'id': ['1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2',
'5', '5', '5','5', '5', '5', '5'],
'money': ['15', '15', '15', '21', '21', '21', '21',
'17', '17', '17', '20', '17', '17', '17',
'25', '30', '22', '25', '8', '7', '12']}).astype(int)
这是我的代码:
# for every person
for i in df.id.unique():
# find the first and last index value
first = df[df['id']==i].index.values.astype(int)[0]
last = df[df['id']==i].index.values.astype(int)[-1]
# first element has to be kept
print(False)
# for all elements, compare values next to each other
while first < last:
abs_diff = abs( df['money'][first] - df['money'][first+1] ) > 0
# print TRUE, when adjacent values differ
print(abs_diff)
# update the counter
first +=1
它返回一个布尔列表,即: FalseFalseFalseTrueFalseFalseFalseFalseFalseFalseFalseTrueTrueFalseFalseFalseTrueTrueTrueTrueTrueTrueTrue
问题:如何将循环输出保存在列表中?
答案 0 :(得分:2)
df.groupby('id')['money'].diff().fillna(0).ne(0).to_list()
[出]
[False,
False,
False,
True,
False,
False,
False,
False,
False,
False,
True,
True,
False,
False,
False,
True,
True,
True,
True,
True,
True]
答案 1 :(得分:1)
尝试
import pandas as pd
df = pd.DataFrame({'year': ['2001', '2004', '2005', '2006', '2007', '2008', '2009',
'2003', '2004', '2005', '2006', '2007', '2008', '2009',
'2003', '2004', '2005', '2006', '2007', '2008', '2009'],
'id': ['1', '1', '1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2',
'5', '5', '5','5', '5', '5', '5'],
'money': ['15', '15', '15', '21', '21', '21', '21',
'17', '17', '17', '20', '17', '17', '17',
'25', '30', '22', '25', '8', '7', '12']}).astype(int)
# for every person
l=list()
for i in df.id.unique():
# find the first and last index value
first = df[df['id']==i].index.values.astype(int)[0]
last = df[df['id']==i].index.values.astype(int)[-1]
# first element has to be kept
print(False)
l.append(False)
# my try: diff = []
# for all elements, compare values next to each other
while first < last:
abs_diff = abs( df['money'][first] - df['money'][first+1] ) > 0
# print TRUE, when adjacent values differ
l.append(abs_diff)
# my try: diff.append(abs_diff)
# update the counter
first +=1
print(l)
输出:
[False, False, False, True, False, False, False, False, False, False, True, True, False, False, False, True, True, True, True, True, True]
答案 2 :(得分:1)
如果我正确理解了您的问题,则只需在for
循环之外定义变量:
output = list()
for i in df.id.unique():
# find the first and last index value
first = df[df['id']==i].index.values.astype(int)[0]
last = df[df['id']==i].index.values.astype(int)[-1]
# first element has to be kept
output.append(False)
# for all elements, compare values next to each other
while first < last:
abs_diff = abs( df['money'][first] - df['money'][first+1] ) > 0
# print TRUE, when adjacent values differ
output.append(abs_diff)
# update the counter
first +=1
print(output)
此刻,您每次都需要重新设置值,这样最终只能得到最后一个id
的输出。
答案 3 :(得分:1)
from collections import defaultdict
result = defaultdict(list)
for i in df.id.unique():
# find the first and last index value
first = df[df['id']==i].index.values.astype(int)[0]
last = df[df['id']==i].index.values.astype(int)[-1]
# first element has to be kept
print(False)
result.append("False")
# my try: diff = []
# for all elements, compare values next to each other
while first < last:
abs_diff = abs( df['money'][first] - df['money'][first+1] ) > 0
# print TRUE, when adjacent values differ
print(abs_diff)
result[i].append(abs_diff)
# my try: diff.append(abs_diff)
# update the counter
first +=1
我想如果您想为每个人分别保存ID会起作用。