简体   繁体   中英

Combining data sets of different sizes

My problem is next:

I have data frame A that looks like this:

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1

and data frame B that look like this:

2 2 2
2 2 2
2 2 2

and I am trying to add B to A, so result should look like this:

1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 3 3 3 1 1 1 1
1 1 1 3 3 3 1 1 1 1
1 1 1 3 3 3 1 1 1 1
1 1 1 1 1 1 1 1 1 1

At the moment I am extending data set B with zeros so size it gets equal in sizes with data set A..... and just sum those two data sets.

Is there more elegant way of doing this? Also how I can add data set B to the different parts of data set A.... and for example will have result like this:

3 3 3 1 1 1 1 1 1 1
3 3 3 1 1 1 1 1 1 1
3 3 3 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1  

Let me know if my question is not clear:)

Thanks.

for the first one:

A.iloc[2:4, 2:4] =  A.iloc[2:4, 2:4] + B.to_numpy()

and for the second one:

A.iloc[0:2, 0:2] =  A.iloc[2:4, 2:4] + B.to_numpy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM