'How to name columns and rows of a Matrix with names embeded in python?
I need to name columns and rows of a matrix with those the columns names embeded in the matrix:
Example:
M = ['A:1' 'B:0' 'C:0', 'A:0' 'B:1' 'C:0', 'A:0' 'B:0' 'C:1']
to:
M = ['1' 'A' 'B' 'C','2' '1' '0' '0','3' '0' '1' '0', '4' '0' '0' '1']
Much like Excel format where the columns are labeled by the variables embeded in the matrix, and the rows are numbered.
This code should be used later on in analysis of big data
Solution 1:[1]
The pandas library extends numpy arrays to give you a data representation (a DataFrame) that is closer to Excel.
import pandas as pd
df = pd.DataFrame({'A': [1, 0, 0],
'B': [0, 1, 0],
'C': [0, 0, 1]})
print(df)
Output:
A B C
0 1 0 0
1 0 1 0
2 0 0 1
It also has a read_excel() function and DataFrame.to_excel() method.
To extract the underlying numpy array from a DataFrame, use the .values attribute:
print(df.values)
Output:
[[1 0 0]
[0 1 0]
[0 0 1]]
Solution 2:[2]
Use Pandas DataFrame-
df = pd.DataFrame(index=row_names_list, columns=cols_names_list)
E.g.
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | motik |
