程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[pandas] array to dataframe, dataframe to JSON, dataframe modify column order

編輯:Python

Record some problems encountered in the project .

import numpy as np
a = np.array((0.2,0.5,0.9,1.5))


Yes a To transpose :

import numpy as np
a = np.array((0.2,0.5,0.9,1.5))
a_new = a.reshape(1,4)

1、array turn DataFrame
import numpy as np
import pandas as pd
a = np.array((0.2,0.5,0.9,1.5))
a_new = a.reshape(1,4)
df_a = pd.DataFrame(a_new)


Change column names :

import numpy as np
import pandas as pd
a = np.array((0.2,0.5,0.9,1.5))
a_new = a.reshape(1,4)
df_a = pd.DataFrame(a_new)
df_a.columns = ['a', 'b', 'c', 'd'] # Name 

2、DataFrame turn json
import numpy as np
import pandas as pd
a = np.array((0.2,0.5,0.9,1.5))
a_new = a.reshape(1,4)
df_a = pd.DataFrame(a_new)
df_a.columns = ['a', 'b', 'c', 'd'] # Name 
# DataFrame turn jason
df_a_new = df_a.to_json(orient="columns")
3、DataFrame Change the order of columns

stay df_a The order of columns in is a、b、c、d

The order we need now is d、b、c、a

import numpy as np
import pandas as pd
a = np.array((0.2,0.5,0.9,1.5))
a_new = a.reshape(1,4)
df_a = pd.DataFrame(a_new)
df_a.columns = ['a', 'b', 'c', 'd'] # Name 
df_a_p = df_a[["d","b","c","a"]]


attach , Give away one
Splicing DataFrame,A,B,C, D four DataFrame
ABCD = pd.concat([A,B,C, D], axis=1)

Reference resources :
https://blog.csdn.net/qq_27328197/article/details/113823989


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved