Multi row assignment with .loc and DataFrame dimension matching
Here is a full solution using .loc of zero indexes and overcomes your dimension/length error
error: 'cannot set using a list-like indexer with a different length than the value'
To match the dimensions, create a DataFrame of the zero arrays in the shape you want/need when you assign to the zero indexes instead of assigning the raw arrays.
import numpy as np
import pandas as pd
from cStringIO import StringIO
# Create example DataFrame
df_text = '''
078401115X| 0
0790747324| 0
0790750708|[(354, 1), (393, 1), (447, 1), (642, 1), (886,1)]
0800103688| 0
5556167281|[(41, 1), (86, 1), (341, 1), (362, 1), (419, 10)]
6300157423| 0
6300266850| 0
6301699599| 0
6301723465| 0
'''
df = pd.read_table(StringIO(df_text), sep='|', index_col=0, header=None, skipinitialspace=True)
print 'Original DataFrame:'
print df
print
# Find indexes with zero data in first column
zero_indexes = df[df[1] == '0'].index
print 'Zero Indexes:'
print zero_indexes.tolist()
print
# Assign numpy zero array to indexes
df.loc[zero_indexes] = pd.DataFrame([[np.zeros(4)]], index=zero_indexes, columns=[1])
print 'New DataFrame:'
print df
Original DataFrame:
1
0
078401115X 0
0790747324 0
0790750708 [(354, 1), (393, 1), (447, 1), (642, 1), (886,1)]
0800103688 0
5556167281 [(41, 1), (86, 1), (341, 1), (362, 1), (419, 10)]
6300157423 0
6300266850 0
6301699599 0
6301723465 0
Zero Indexes:
['078401115X', '0790747324', '0800103688', '6300157423', '6300266850', '6301699599', '6301723465']
New DataFrame:
1
0
078401115X [0.0, 0.0, 0.0, 0.0]
0790747324 [0.0, 0.0, 0.0, 0.0]
0790750708 [(354, 1), (393, 1), (447, 1), (642, 1), (886,1)]
0800103688 [0.0, 0.0, 0.0, 0.0]
5556167281 [(41, 1), (86, 1), (341, 1), (362, 1), (419, 10)]
6300157423 [0.0, 0.0, 0.0, 0.0]
6300266850 [0.0, 0.0, 0.0, 0.0]
6301699599 [0.0, 0.0, 0.0, 0.0]
6301723465 [0.0, 0.0, 0.0, 0.0]
df.loc[df['col']==0, 'col'] = df.index