简体   繁体   中英

set entire row to value

I have a dataframe atype

    AT  N   term
1   4   1   1
2   3   2   1
3   2   3   2
4   1   4   2

i need to set the entire row of a new dataframe to term value from atype . Is this the best way to accomplish the task?

m1 = pd.DataFrame(0, index = atype.index, columns=atype.index)
m2 = pd.DataFrame(0, index = atype.index, columns=atype.index)


for i in range(1, atype.shape[0]+1):
    m1.ix[i] = atype.ix[i,"term"]
    m2[i] = atype.ix[i,"term"]

or is there a better/ more efficient way in where i dont need to use a loop?

Is that what you want?

In [91]: pd.DataFrame([atype.term.values.tolist()] * len(atype),
                      columns=atype.index, index=atype.index).T
Out[91]:
   1  2  3  4
1  1  1  1  1
2  1  1  1  1
3  2  2  2  2
4  2  2  2  2

In [92]: pd.DataFrame([atype.term.values.tolist()] * len(atype),
                      columns=atype.index, index=atype.index)
Out[92]:
   1  2  3  4
1  1  1  2  2
2  1  1  2  2
3  1  1  2  2
4  1  1  2  2

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