python - Adding empty columns to python DataFrame
问题描述
I am trying to add couple of empty columns to a python Dataframe , the columns to be added are in the form of list, how could I do it
list=[4,5,6]
data1={'77':[1.2,2.2,3.5],'18':[4.5,6.5,9.9],'89':[8.8,9.8,7.7]}
Input=pd.DataFrame(data1)
Input
data2={'77':[1.2,2.2,3.5],'18':[4.5,6.5,9.9],'89':[8.8,9.8,7.7],'4':[np.NaN,np.NaN,np.NaN],'5':[np.NaN,np.NaN,np.NaN],'6':[np.NaN,np.NaN,np.NaN]}
Output=pd.DataFrame(data2)
Output
What I thought of doing: I cant manually add each column one at a time to dataframe as in my usecase we will be adding 1000+ empty columns, I tried of running through a loop but it dint work out
解决方案
做这个:
List = [4, 5, 6]
data = {'77':[1.2, 2.2, 3.5], '18':[4.5, 6.5, 9.9], '89':[8.8, 9.8, 7.7]}
for i in List: # for create empty columns with column names from the list
Input[i] = np.nan
Input
输出:
77 18 89 4 5 6
0 1.2 4.5 8.8 NaN NaN NaN
1 2.2 6.5 9.8 NaN NaN NaN
2 3.5 9.9 7.7 NaN NaN NaN
推荐阅读
- apache-spark - Databricks 连接:sparkContext.wholeTextFiles
- c# - 异步方法中的 Task.Run() 导致线程池饥饿?
- node.js - TypeError:“x”不是节点js中的函数
- javascript - 如何将 JSON 数据从 ejs 传递到普通的内联 javascript
- html - 如何制作适合圆形导航栏的圆形 div?
- angular - ngx-infinite-scroll 调用函数只有 2 次?
- package - Ocaml - 尝试编译时出现语法错误
- azure - Azure VM 扩展更新失败
- amazon-cloudformation - 在 AWS CDK 中使用 Parameter Store 值的最佳方式是什么
- azure - 是否可以在位于 Github 的 YML 中签出 Gitlab 存储库?