2018年4月19日 星期四

Python - pandas dataframe的append處理後保留欄位名稱的順序 - How to control column order when appending to a DataFrame

Information:

System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

Code:

import pandas as pd

df1 = pd.DataFrame()
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df1['number'] = number
df1['sex'] = sex
df1['b'] = [1,2,3,4,5]

df2 = pd.DataFrame()
number = [1,2,3,4,5]
sex = ['male','female','female','female','male']
df2['number'] = number
df2['sex'] = sex
df2['a'] = [1,2,3,4,5]
print('df1')
print(df1)
print('-'*45)
print('df2')
print(df2)
print('-'*45)

print('未處理欄位名稱,欄位名稱重新排序')
df3 = df1.append(df2,ignore_index =True)
print(df3)
print('-'*45)

print('處理後,欄位名稱會以df1為準,新的欄位依序加入')

columns_all = list(df1.columns)
columns_new = list(df2.columns)

for ele in columns_new:
    if ele not in columns_all:
        columns_all.append(ele)

df3 = df1.append(df2,ignore_index =True)
df3 = df3[columns_all]
print(df3)
print('-'*45)

Result:

df1
   number     sex  b
0       1    male  1
1       2  female  2
2       3  female  3
3       4  female  4
4       5    male  5
---------------------------------------------
df2
   number     sex  a
0       1    male  1
1       2  female  2
2       3  female  3
3       4  female  4
4       5    male  5
---------------------------------------------
未處理欄位名稱,欄位名稱重新排序
     a    b  number     sex
0  NaN  1.0       1    male
1  NaN  2.0       2  female
2  NaN  3.0       3  female
3  NaN  4.0       4  female
4  NaN  5.0       5    male
5  1.0  NaN       1    male
6  2.0  NaN       2  female
7  3.0  NaN       3  female
8  4.0  NaN       4  female
9  5.0  NaN       5    male
---------------------------------------------
處理後,欄位名稱會以df1為準,新的欄位依序加入
   number     sex    b    a
0       1    male  1.0  NaN
1       2  female  2.0  NaN
2       3  female  3.0  NaN
3       4  female  4.0  NaN
4       5    male  5.0  NaN
5       1    male  NaN  1.0
6       2  female  NaN  2.0
7       3  female  NaN  3.0
8       4  female  NaN  4.0
9       5    male  NaN  5.0
---------------------------------------------

沒有留言:

張貼留言