László
László

Reputation: 4144

getting pandas Setting With Enlargement right

Since version 0.13, it is possible to append to a dataframe by referring to indices in .loc or .ix which are not yet in the dataframe. See the documentation.

Then I am confused why this line fails:

all_treatments.loc[originalN:newN,:]    = all_treatments.loc[0:newrowcount,:]

This generates a ValueError:

ValueError: could not broadcast input array from shape (12) into shape (0)

Here all_treatments.shape = (53, 12), originalN = 53, newN = 64, all_treatments.loc[originalN:newN,:].shape = (0,12), all_treatments.loc[0:newrowcount,:].shape = (12,12).

What is the proper way to set with enlargement here?

Upvotes: 2

Views: 1261

Answers (1)

Alexander
Alexander

Reputation: 109696

You can only set by enlargement with a single row or column. You are setting with a range.

The .loc/.ix/[] operations can perform enlargement when setting a non-existant key for that axis.

For your use, something like this should work to expand a dataframe with new blank rows:

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
>>> df
    a   b
0   1   4
1   2   5
2   3   6

new_row_count = 2
for new_row, old_row in enumerate(range(new_row_count), start=len(df)):
    df.ix[new_row] = None

>>>df
    a   b
0   1   4
1   2   5
2   3   6
3   NaN NaN
4   NaN NaN

If you wanted to copy data from the original dataframe, I would normally just concatenate.

df = pd.concat([df, df.iloc[:2, :]], ignore_index=True)

Upvotes: 1

Related Questions