Convert Array to DataFrame in Python

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

When I enter the code above, I get this answer:

enter image description here

But how to change the column name?

+6
source share
2 answers

You can add parameter columns or use dictwith a key that translates to a 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
+7
source

In general, you can use the panda rename function here . Given your data frame, you can change to a new name like this. If you have more columns, you can also rename them in the dictionary. 0 - current name of your column

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

e_dataframe.rename(index=str, columns={0:'new_column_name'})
0
source

Source: https://habr.com/ru/post/1682064/


All Articles