因此,我想要完成的是,每次在数据帧中发现一行缺少日期和前五列的其余部分时,复制其上一行的值。示例:
0| Date | Name | Amount | Address
1| 12/04/2018| Pepe | $1.00 | Avenue 1
2| NaT | NaN | NaN | NaN (In this line i need the values of the line above)
3| 1/04/2018 | Tito | $3.00 | Avenue 2
for file in files:
fileName = os.path.splitext(file)[0]
if fileName == 'xxxxxxx (copy)':
df = pd.read_excel(file)
for index, row in df.iterrows():
if pd.isna(df['Date'] 'And the rest of the 5 columns'):
#Copy the values of the line above it
使用ffill()
结转以上行。
示例:
# Given
df = pd.DataFrame({'word':['Alpha', np.NaN, 'Charlie'],
'Percentage 1':[10, np.NaN, 0],
'Percentage 2': [5, np.NaN, 4]})
df
word Percentage 1 Percentage 2
0 Alpha 10.0 5.0
1 NaN NaN NaN
2 Charlie 0.0 4.0
df = df.ffill()
word Percentage 1 Percentage 2
0 Alpha 10.0 5.0
1 Alpha 10.0 5.0
2 Charlie 0.0 4.0
所以我使用ffill(正向填充(方法来解决它,下面是代码:
for file in files:
fileName = os.path.splitext(file)[0]
if fileName == 'File1':
df = pd.read_excel(file)
new_df = df.fillna(method="ffill")
writer = pd.ExcelWriter('File1.xlsx', engine='xlsxwriter')
new_df.to_excel(writer, 'Sheet 1')
writer.save()