Replacing Empty Elements with NA in a Pandas DataFrame Using List Operations
import pandas as pd
# Create a sample DataFrame from the given data
data = {
'col1': [1, 2, 3, 4],
'col2': ['c001', 'c001', 'c001', 'c001'],
'col3': [11, 12, 13, 14],
'col4': [['', '', '', '5011'], [None, None, None, '']]
}
df = pd.DataFrame(data)
# Define a function to replace length-0 elements with NA
def replace_zero_length(x):
return x if len(x) > 0 else [None] * (len(x[0]) - 1) + [x[-1]]
# Apply the function to the 'col4' column and repeat its values based on the number of rows for each list
df['col4'] = df['col4'].apply(replace_zero_length)
# Repeat the values in 'col5' as well, based on the length of their lists
df['col5'] = [x[-1] if len(x) == 1 else x for x in df['col5'].apply(list)]
# Print the resulting DataFrame
print(df)
Last modified on 2024-05-26