赞
踩
一、直接添加
代码:
- import pandas as pd
-
- test1 = pd.DataFrame([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]],columns=list('ABCD')) #
- print(test1)
- test1['E']=[6,7,8,9]
- print(test1)
二、通过concat函数添加
- import pandas as pd
-
- test1 = pd.DataFrame([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]],columns=list('ABCD')) #
- print(test1)
- test2=pd.DataFrame([8,9,10,11],columns=list('F'))
- test1=pd.concat([test1,test2],axis=1)
- print(test1)
三、reindex函数添加
- import pandas as pd
-
- test1 = pd.DataFrame([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]],columns=list('ABCD')) #
- print(test1)
-
- test3=test1.reindex(columns=list('MABCDN'),fill_value=1)
- print(test3)
四、assign函数
- import pandas as pd
- import numpy as np
-
- df = pd.DataFrame({'A':range(1, 6), 'B':np.random.randn(5)})
- print(df)
-
- df01=df.assign(dd=range(5,10))
- print(df01)
五、loc函数添加
- import pandas as pd
-
- test1 = pd.DataFrame([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]],columns=list('ABCD')) # 自己输入
- print(test1)
-
- test2=[False,False,True,False]
- test2=pd.Series(test2)
-
- test1.loc[test2,'E']=1
- print(test1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。