在 python 中用 "list of lists" 中的前一个值替换零



我有一个名为"名称"的列表。

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]]

在名称列表中,如果年份列(索引 7(等于 0,那么我想用上一年的值替换它。例如:第三个列表的年份值是 0,我想用 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中被弃用了。

尝试:

year = 0
for ns in Names:
ns[7] = year if ns[7] == 0 else ns[7]
year = ns[7]

你可以试试这个:

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)

解释:

使用for-looprange(len())length次数

for ele in range(len(Names)): #it will iterate over three times as len -> 3

接下来检查索引7处的year值,如果它等于0则复制上一年意味着上一个迭代步骤中存在的年份表示ele-1。例如,如果它在 ele(第 2 次迭代(中,那么它将从ele-1(第 1 次迭代(中获取年份。

Names[ele][7] = (Names[ele-1][2].split('-'))[0]

年份以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']]

Pandas 包具有此功能,对于对表格数据的许多其他类型的操作很有用。

如果您愿意使用它,您可以按如下方式解决此填充问题:

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)

输出:

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所指出的,Series.replace有一个方法参数,因此您可以一次性完成操作,如下所示:

df['Year'] = df['Year'].replace(0, method='ffill')

您可以使用解包:

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]]

最新更新