问题遇到的现象和发生背景
有两列数据,数据非常多,如表格,要将空白处填充为表格中对映的内容?
有两列数据,数据非常多,如表格,要将空白处填充为表格中对映的内容?
感觉自己写的和屎一样
import pandas as pd
from math import nan, isnan
data = pd.DataFrame({
'A': [f'A{i:03}' for i in range(1, 14)] + [f'A{i:03}' for i in range(1, 6)] + [f'A{i:03}' for i in range(19, 26)],
'B': [f'B10000{i:02}' for i in range(13)] + [nan for i in range(5)] + [f'B0000{i}' for i in range(22, 29)]
})
data_tmp = data.dropna()
dic = dict(zip(data_tmp['A'], data_tmp['B']))
for i in range(len(data)):
if (not isinstance(data.iloc[i, 1], str)) and isnan(data.iloc[i, 1]):
data.iloc[i, 1] = dic[data.iloc[i, 0]]
data.to_excel('res.xlsx', index = False)