How to simply add a column level to a pandas dataframe

pandas add multi level column
pandas add level to multiindex
pandas concat
pandas add column
pandas add constant to column
pandas multiindex columns
pandas add value to column
pandas multiindex add column

let say I have a dataframe that looks like this:

df = pd.DataFrame(index=list('abcde'), data={'A': range(5), 'B': range(5)})
 df
Out[92]: 
   A  B
a  0  0
b  1  1
c  2  2
d  3  3
e  4  4

Asumming that this dataframe already exist, how can I simply add a level 'C' to the column index so I get this:

 df
Out[92]: 
   A  B
   C  C
a  0  0
b  1  1
c  2  2
d  3  3
e  4  4

I saw SO anwser like this python/pandas: how to combine two dataframes into one with hierarchical column index? but this concat different dataframe instead of adding a column level to an already existing dataframe.

-

As suggested by @StevenG himself, a better answer:

df.columns = pd.MultiIndex.from_product([df.columns, ['C']])

print(df)
#    A  B
#    C  C
# a  0  0
# b  1  1
# c  2  2
# d  3  3
# e  4  4

Hierarchical Indexing, Notice that the MultiIndex contains multiple levels of indexing–in this case, the state Concretely, we might want to add another column of demographic data for with a MultiIndex this is as easy as adding another column to the DataFrame :. So first let's create a data frame using pandas series. In the below example we are converting a pandas series to a Data Frame of one column, giving it a column name Month_no. Example. Live Demo. import pandas as pd s = pd.Series([6,8,3,1,12]) df = pd.DataFrame(s,columns=['Month_No']) print (df) Output. Running the above code gives us the following result:

option 1 set_index and T

df.T.set_index(np.repeat('C', df.shape[1]), append=True).T

option 2 pd.concat, keys, and swaplevel

pd.concat([df], axis=1, keys=['C']).swaplevel(0, 1, 1)

Python, Syntax: DataFrame.add(other, axis='columns', level=None, fill_value=None). Parameters: other :Series, DataFrame, or constant axis :{0, 1, 'index', 'columns'}  Output: This method will create a new dataframe with new column added to the old dataframe. We can use a Python dictionary to add a new column in pandas DataFrame. Use an existing column as the key values and their respective values will be the values for new column. # value pairs as the # values for our new column.

Another way for MultiIndex (appanding 'E'):

df.columns = pd.MultiIndex.from_tuples(map(lambda x: (x[0], 'E', x[1]), df.columns))

   A  B
   E  E
   C  D
a  0  0
b  1  1
c  2  2
d  3  3
e  4  4

Merge, join, and concatenate, pandas provides various facilities for easily combining together Series or It is worth noting that concat() (and therefore append() ) makes a full copy of the left_on : Columns or index levels from the left DataFrame or Series to use as keys​. Syntax – Add Column. The syntax to add a column to DataFrame is: mydataframe['new_column_name'] = column_values. where mydataframe is the dataframe to which you would like to add the new column with the label new_column_name.

pandas.DataFrame.add, DataFrame. add (self, other, axis='columns', level=None, fill_value=None)[source​]¶. Get Addition of dataframe and other, element-wise (binary operator add ). Add a new column in DataFrame with same default value Now add a new column ‘Total’ with same value 50 in each index i.e dfObj['Total'] = 50 It will add the new column ‘Total’ and set value 50 at each index in that column.

3 Ways to Add New Columns to Pandas Dataframe?, How to Add Group-Level Summary Statistic as a New Column in Pandas? Default Thumbnail 6 ways to Sort Pandas Dataframe: Pandas Tutorial. Stack Overflow Public questions and answers; merge two dataframes and add column level with names. Get list from pandas DataFrame column headers. 189.

A brief guide to pandas multi-index dataframes, Most data sets have a single variable for the DataFrame index. As a simple example, lets make a multi-index DataFrame to imitate a particle distribution function. df1 and df2 , and want to put these in together with a new multiindex level:. Add fourth_index as a column and then call set_index. The name is preserved. df = df.assign(d=fourth_index).set_index('d', append=True) Note, if you're worried about memory, what you're doing is fine as is. No point sacrificing performance for a fewer characters.

Comments
  • this is great, I like pd.MultiIndex.from_product([df.columns, ['C']]) which is a bite more trivial since you don't have to keep track of the len of df.columns. you mind adding it to the answer so I can accept it?
  • @StevenG great I did not know this trick. Thanks I have learned something new :-)
  • Do you have any tips, how to add another level, when the original df already has multiindex column names? I tried to add new level with from_product() method, however I received this error message: 'NotImplementedError: isnull is not defined for MultiIndex'.
  • @LenkaVraná pd.MultiIndex.from_product(df.columns.levels + [['C']])
  • For anyone. I found casting the existing columns index to list before using it in MultiIndex.from_product works for 'isna not implemented'. pd.MultiIndex.from_product([list(df.columns), ['C']])
  • Thanks did not know about swap and this is convinient. I tested it for a large dataframe to see if it was more efficient than setting pd.MultiIndex.from_product([df.columns, ['C']]) and it was about 25% slower.
  • No surprises! Romain's answer is quicker. I added this because I think it's valuable to know.
  • pd.concat([df], axis=1, keys=['C']) worked very well for multilevel columns
  • Option 2 should be the accepted answer for the general case when df.columns can be a pd.MultiIndex.
  • The pd.concat answer is great because it doesn't modify the original df.
  • A shorter version: df.columns = pd.MultiIndex.from_tuples([(c[0], 'E', c[1]) for c in df.columns])