我有以下代码:
db_fields = ("id", "email", "status", "source")
df = DataFrame(results)
for col in db_fields:
if col not in df.columns:
COLUMN IS MISSING - COMMAND TO ADD COLUMN
例如,如果缺少status
列,则需要将其添加到数据框中而没有任何值,因此,当我将df
导出到csv
时,我将始终具有相同的架构领域。
我知道要删除列,我应该这样做:
df = df.drop(col, 1)
但是我不知道添加具有空值的列的最佳方法是什么。
答案 0 :(得分:1)
此方法将为状态列添加Null值:
import numpy as np
df['status'] = np.nan
或者:
df['status'] = None
所以:
db_fields = ("id", "email", "status", "source")
for col in db_fields:
if col not in df.columns:
df[col] = None
答案 1 :(得分:1)
您可以创建不存在的列的数组,并使用assign
和字典创建新的列:
df = pd.DataFrame({'id': ['a1','a2', 'b1'],
'a': ['a1','a2', 'b1'],
'source': ['a1','a2', 'b1']})
print (df)
id a source
0 a1 a1 a1
1 a2 a2 a2
2 b1 b1 b1
db_fields = ("id", "email", "status", "source")
#get missing columns
diff = np.setdiff1d(np.array(db_fields), df.columns)
print (diff)
['email' 'status']
#get original columns not existed in db_fields
diff1 = np.setdiff1d(df.columns, np.array(db_fields)).tolist()
print (diff1)
['a']
#add missing columns with change order
d = dict.fromkeys(diff, np.nan)
df = df.assign(**d)[diff1 + list(db_fields)]
print (df)
a id email status source
0 a1 a1 NaN NaN a1
1 a2 a2 NaN NaN a2
2 b1 b1 NaN NaN b1
#if necessary first db_fields
df = df.assign(**d)[list(db_fields) + diff1]
print (df)
id email status source a
0 a1 NaN NaN a1 a1
1 a2 NaN NaN a2 a2
2 b1 NaN NaN b1 b1
答案 2 :(得分:1)
在这里,只需一行即可简单明了:
import numpy as np
db_fields = ("id", "email", "status", "source")
df = DataFrame(results)
for col in db_fields:
if col not in df.columns:
# Add the column
df[col] = np.nan
顺便说一句::您还可以使用df.drop(inplace=True)
删除一列。