Must have equal len keys and value when setting with an iterable error

35 views Asked by At

while writing the data into dataframe I faced this issue

ValueError: Must have equal len keys and value when setting with an iterable.

This csv has 98 rows in which I'm trying to assign values to the columns which i took as a list variable

variables = [positive_score,
            negative_score,
            polarity_score,
            subjectivity_score]
# write the values to the dataframe
var = var[:98]
for i, var in enumerate(variables):
  output_df.loc[i:97] = var
output_df.to_csv('Output_data.csv', index=False)
1

There are 1 answers

0
Chathura Abeywickrama On

you can use the iloc method to assign values row-wise.

variables = [positive_score, negative_score, polarity_score, subjectivity_score]

# Ensure all columns have the same length
min_length = min(len(var) for var in variables)
variables = [var[:min_length] for var in variables]

# Write the values to the dataframe row-wise using iloc
for i, var in enumerate(variables):
    output_df.iloc[:, i] = var

output_df.to_csv('Output_data.csv', index=False)