feng_h feng_h - 2 years ago 366
Python Question

convert array into DataFrame in Python

import pandas as pd
import numpy as np
e = np.random.normal(size=100)
e_datafrmae = pd.DataFrame(e)


When I input the codes above, I get this answer:

enter image description here

But how to change the columns name?

Answer Source

You can add parameter columns or use dict with key which is converted to column name:

np.random.seed(123)
e = np.random.normal(size=10)  
dataframe=pd.DataFrame(e, columns=['a']) 
print (dataframe)
          a
0 -1.085631
1  0.997345
2  0.282978
3 -1.506295
4 -0.578600
5  1.651437
6 -2.426679
7 -0.428913
8  1.265936
9 -0.866740

e_dataframe=pd.DataFrame({'a':e}) 
print (e_dataframe)
          a
0 -1.085631
1  0.997345
2  0.282978
3 -1.506295
4 -0.578600
5  1.651437
6 -2.426679
7 -0.428913
8  1.265936
9 -0.866740
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download