Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

python - Multiple insert columns if not exist pandas

I have the following df

list_columns = ['A', 'B', 'C']
list_data = [
    [1, '2', 3],
    [4, '4', 5],
    [1, '2', 3],
    [4, '4', 6]
    ]
df = pd.DataFrame(columns=list_columns, data=list_data)

I want to check if multiple columns exist, and if not to create them.

Example: If B,C,D do not exist, create them(For the above df it will create only D column) I know how to do this with one column:

if 'D' not in df:
    df['D']=0

Is there a way to test if all my columns exist, and if not create the one that are missing? And not to make an if for each column

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here loop is not necessary - use DataFrame.reindex with Index.union:

cols = ['B','C','D']

df = df.reindex(df.columns.union(cols, sort=False), axis=1, fill_value=0)
print (df)
   A  B  C  D
0  1  2  3  0
1  4  4  5  0
2  1  2  3  0
3  4  4  6  0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...