In Python, batch replacement of null values or specified values in Excel tables can usually be done bypandas
Library combinationopenpyxl
(for writing.xlsx
to implement it. Here will show how to use itpandas
To read the Excel file, replace the empty value or specified value, and write the result back to the new Excel file.
1. Install the necessary libraries
If you haven't installed itpandas
andopenpyxl
, you can install them via pip:
pip install pandas openpyxl
2. Replace empty value
Suppose we want to put all null values in Excel (NaN
) Replace with a specific value (e.g.0
or"N/A"
)。
import pandas as pd # Read Excel filedf = pd.read_excel('', sheet_name='Sheet1') # Replace all null values to 0 (or whatever value you want)(0, inplace=True) # If you want to replace it with a string, make sure the data type is correct# ('N/A', inplace=True) # Write the result back to a new Excel filedf.to_excel('modified_example.xlsx', sheet_name='Sheet1', index=False, engine='openpyxl')
3. Replace the specified value
If you want to replace a specified value in Excel (such as all"Old Value"
Replace with"New Value"
), can be usedreplace
method.
import pandas as pd # Read Excel filedf = pd.read_excel('', sheet_name='Sheet1') # Replace the specified value('Old Value', 'New Value', inplace=True) # Write the result back to a new Excel filedf.to_excel('modified_example.xlsx', sheet_name='Sheet1', index=False, engine='openpyxl')
Things to note
-
inplace=True
Parameters mean that the replacement operation will directly modify the original DataFrame.df
instead of returning a new DataFrame. If you do not want to modify the original data, you can omit this parameter and save the replaced DataFrame to a new variable. - In use
to_excel
When the method,index=False
Parameters are used to prevent the index of DataFrame from being written to Excel files as well. If you need to keep the index, you can omit this parameter or set it toTrue
。 -
engine='openpyxl'
Parameters are written.xlsx
Recommended settings for files, which allowspandas
useopenpyxl
The library functions to handle.xlsx
document. - If you have memory problems when dealing with large Excel files, you can consider using
chunksize
Parameters are inread_excel
The data is read in batches and processed block by block. But this may not be very direct for a replacement operation, because you need to merge the results back into a DataFrame. In this case, other solutions may need to be considered, such as usingxlsxwriter
Library (but please note,xlsxwriter
Reading Excel files is not supported, only for writing).
This is the article about Python replacing empty values or specified values in Excel tables. For more related content on Python replacing Excel values, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!