Home > database >  Creating Pandas DataFrame Column with Dashes
Creating Pandas DataFrame Column with Dashes

Time:12-08

Background -
I am aware of the various ways to add columns to a pandas DataFrame, such as assign(), insert(), concat() etc.. However, I haven't been able to ascertain how I can add a column (similar to inset() in that I specify the position or index) and have that column populated with dashed values.

Expected DataFrame structure -
Per the following, I am looking to add colC and add - values that span the same length as the columns that do have data.

print(df)
    colA  colB colC
0   True     1    -
1  False     2    -
2  False     3    -

In the above example, as the DataFrame has 3x rows, colC populates with - values for all 3x rows.

CodePudding user response:

It's as simple as it gets.

>>> df
    colA  colB
0   True     1
1  False     2
2  False     3

>>> df['colC'] = '-'
>>> df
    colA  colB colC
0   True     1    -
1  False     2    -
2  False     3    -
  • Related