I'm very new in Python. I want to create amxn matrix and add names to its columns and rows. I have a list contains row names and a list contains column names. It seems that I need to use "Pandas". But I do not know how to use it. Can any body help me?
You just read in the file with index_col=0 indicating those will be your rownames and pandas automatically gets your colnames from the first row.
m = pd.read_csv("mat.txt,delimiter="\t",index_col=0)
you can convert to a numpy matrix using
m.as_matrix()
to sparse matrix using
import scipy.sparse
scipy.sparse.coo_matrix(m.values)
Hope this helps a bit
Use Pandas DataFrame-
df = pd.DataFrame(index=row_names_list, columns=cols_names_list)
Eg
df = pd.DataFrame(index=['a', 'b', 'c'], columns=['aa', 'bb', 'cc', 'dd'])
Output:
aa bb cc dd
a NaN NaN NaN NaN
b NaN NaN NaN NaN
c NaN NaN NaN NaN
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.