Say I have a dataframe like below:
df = pd.DataFrame({0:['Hello World!']}) # here df could have more than one column of data as shown below
df = pd.DataFrame({0:['Hello World!'], 1:['Hello Mars!']}) # or df could have more than one row of data as shown below
df = pd.DataFrame({0:['Hello World!', 'Hello Mars!']})
and I also have a list of column names like below:
new_col_names = ['a','b','c','d'] # here, len(new_col_names) might vary like below
new_col_names = ['a','b','c','d','e'] # but we can always be sure that the len(new_col_names) >= len(df.columns)
Given that, how could I replace the column names in df
such that it results something like below:
df = pd.DataFrame({0:['Hello World!']})
new_col_names = ['a','b','c','d']
# result would be like this
a b c d
Hello World! (empty string) (empty string) (empty string)
df = pd.DataFrame({0:['Hello World!'], 1:['Hello Mars!']})
new_col_names = ['a','b','c','d']
# result would be like this
a b c d
Hello World! Hello Mars! (empty string) (empty string)
df = pd.DataFrame({0:['Hello World!', 'Hello Mars!']})
new_col_names = ['a','b','c','d','e']
a b c d e
Hello World! (empty string) (empty string) (empty string) (empty string)
Hellow Mars! (empty string) (empty string) (empty string) (empty string)
From reading around StackOverflow answers such as this, I have a vague idea that it could be something like below:
df[new_col_names] = '' # but this returns KeyError
# or this
df.columns=new_col_names # but this returns ValueError: Length mismatch (of course)
If someone could show me, a way to overwrite existing dataframe column name and at the same time add new data columns with empty string values in the rows, I'd greatly appreciate the help.