用“中的上一个值替换零”;名单;用python
我有一张名为“姓名”的名单 在名称列表中,如果年份列(索引7)等于0,则我想用上一年的值替换它。例如:第三个列表的年份值为0,我想用1742替换它。您可以使用解包:用“中的上一个值替换零”;名单;用python,python,Python,我有一张名为“姓名”的名单 在名称列表中,如果年份列(索引7)等于0,则我想用上一年的值替换它。例如:第三个列表的年份值为0,我想用1742替换它。您可以使用解包: Names = [['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burk
Names = [['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 0]]
new_names = [b+[Names[i-1][-1]] if not a else [*b, a] for i, [*b, a] in enumerate(Names)]
输出:
[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 1742]]
[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', '1742']]
A B C D E F G Year
0 Bassett Richard 1745-04-02 M sen DE Anti-Administration 1745
1 Bland Theodorick 1742-03-21 M rep VA 1742
2 Burke Aedanus 1743-06-16 M rep SC 1742
您可以尝试以下方法:
Names = [['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745],
['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742],
['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 0]]
for ele in range(len(Names)):
if Names[ele][7] == 0:
Names[ele][7] = (Names[ele-1][2].split('-'))[0]
print(Names)
说明:
对循环使用和范围(len())
i在长度上重复次数
for ele in range(len(Names)): #it will iterate over three times as len -> 3
下一步检查索引7
处的year
值,如果它等于0
,则复制上一年意味着上一迭代步骤中存在的年份意味着ele-1
。例如,如果它在ele(第二次迭代)中,则它将从ele-1
(第一次迭代)中获取年份
年份以date
格式组合。要仅检索年份,请使用split()
以-
作为分隔符拆分字符串
'1742-03-21' -> [1742, 03, 21]
因此,年份在索引0
中
(Names[ele-1][2].split('-'))[0] -> we get year from here
最后将当前的ele
年份更新为我们得到的年份
输出:
[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 1742]]
[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', '1742']]
A B C D E F G Year
0 Bassett Richard 1745-04-02 M sen DE Anti-Administration 1745
1 Bland Theodorick 1742-03-21 M rep VA 1742
2 Burke Aedanus 1743-06-16 M rep SC 1742
您需要存储最新的有效年份并将其传递到下一个阶段。reduce
将一个值从一个阶段传递到下一个阶段,由于列表是通过引用传递的,因此我们可以在适当的位置修改列表
Names = [['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745],
['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742],
['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 0]]
def fill_year(year, ns):
if ns[7] == 0:
ns[7] = year
return ns[7]
reduce(fill_year, Names, 0)
print Names
显然,reduce
在python3中被弃用
尝试:
该软件包有一个用于此目的的函数,对于表格数据上的许多其他类型的操作非常有用
如果您愿意使用它,您可以按如下方式解决此填充问题:
import pandas as pd
df = pd.DataFrame(Names, columns=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'Year'])
df['Year'] = df['Year'].replace({0: None}).fillna(method='ffill')
print(df)
df['Year'] = df['Year'].replace(0, method='ffill')
输出:
[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', 1742]]
[['Bassett', 'Richard', '1745-04-02', 'M', 'sen', 'DE', 'Anti-Administration', 1745], ['Bland', 'Theodorick', '1742-03-21', 'M', 'rep', 'VA', '', 1742], ['Burke', 'Aedanus', '1743-06-16', 'M', 'rep', 'SC', '', '1742']]
A B C D E F G Year
0 Bassett Richard 1745-04-02 M sen DE Anti-Administration 1745
1 Bland Theodorick 1742-03-21 M rep VA 1742
2 Burke Aedanus 1743-06-16 M rep SC 1742
更新:
正如@miradulo所指出的,它有一个方法参数,因此您可以按如下方式一次性完成操作:
import pandas as pd
df = pd.DataFrame(Names, columns=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'Year'])
df['Year'] = df['Year'].replace({0: None}).fillna(method='ffill')
print(df)
df['Year'] = df['Year'].replace(0, method='ffill')
@MJP:这就是你所期望的吗?代码运行得很好。但我无法理解这部分:Names[ele][7]=(Names[ele-1][2]。split('-'))[0]@MJP:如果你得到澄清,请告诉我。或者需要更多信息吗?我想你不必在这里拆分日期。你只需要使用“年”即可“指数7中的值。我理解代码@MJP:但是如果连续的元素在索引7处有0
,像这样[['Bassett','Richard','1745-04-02','M','sen','DE','Anti-Administration',1745','Bland','Theodorick','1742-03-21','M','rep VA','0','Burke','Aedanus','1743-06-16','M','rep SC','0]
?这就是为什么我使用split从上一年获取年份element@MJP查看,编号已删除reduce()。如果确实需要,请使用functools.reduce();然而,99%的时间显式for循环可读性更强。嗨,迈克,第二段代码可读性更强。我会同意的。第二个代码实际上更符合我回答这个问题时的目标。用显式的if
语句而不是条件表达式编写fill\u year
这样的代码可能更具可读性。是的,我同意。但是,reduce函数在Python3中不起作用。这就是我没有使用它的原因。对不起,如果我不清楚的话,我的意思是,如果ns[7]==0,那么就不要ns[7]=year,否则ns[7]==0;如果ns[7]==0:ns[7]=year
,那么将作业编码为replace
的to\code>参数。