How to add empty columns to a dataframe?
Question:
I have a dataframe with 38 columns. I want to add 19 more blank columns at the end of it.
I’ve tried this:
df.insert([39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57],
['Quality Control - Dupe Ticketing/Dupe Invoicing',
'Error Type',
'Passed/Not Passed',
'Agent Name',
'Team Leader',
'Responsibility(GO/Market/Capability)',
'Remediation Comments',
'Financial Exposure/Dupe Value(USD)',
'Self Identified (Yes/No)',
'Remediation Date',
'Remediation Status (Yes/No)',
'Remarks',
'CM Impact Yes/No',
'Is product cost(Air+Land) matching accounting lines & excel file -Yes/No',
'For Air - all ticket issued and matches all pax',
'For EXCH Air - passenger association correct & no dupe EXCH',
'Error - Yes/No',
'For EXCH Air - penalty check',
'Comments'],
empty_value)
But this gives a TypeError: unhashable type: 'list'.
Answers:
Insert does not allow for multiple columns. You can try it like this:
df = pd.DataFrame({'col1': [1,2,3], 'col2': ['a','b','c']})
newcols = ['col3', 'col4']
df[newcols] = None
print(df)
# col1 col2 col3 col4
# 0 1 a None None
# 1 2 b None None
# 2 3 c None None
You can add new empty columns doing this:
import pandas as pd
new_columns = ['Quality Control - Dupe Ticketing/Dupe Invoicing',
'Error Type',
'Passed/Not Passed',
'Agent Name',
'Team Leader',
'Responsibility(GO/Market/Capability)',
'Remediation Comments',
'Financial Exposure/Dupe Value(USD)',
'Self Identified (Yes/No)',
'Remediation Date',
'Remediation Status (Yes/No)',
'Remarks',
'CM Impact Yes/No',
'Is product cost(Air+Land) matching accounting lines & excel file -Yes/No',
'For Air - all ticket issued and matches all pax',
'For EXCH Air - passenger association correct & no dupe EXCH',
'Error - Yes/No',
'For EXCH Air - penalty check',
'Comments']
df.loc[:,new_columns] = pd.DataFrame()
Use pd.concat
:
cols = ['Quality Control - Dupe Ticketing/Dupe Invoicing',
'Error Type',
'Passed/Not Passed',
'Agent Name',
'Team Leader',
'Responsibility(GO/Market/Capability)',
'Remediation Comments',
'Financial Exposure/Dupe Value(USD)',
'Self Identified (Yes/No)',
'Remediation Date',
'Remediation Status (Yes/No)',
'Remarks',
'CM Impact Yes/No',
'Is product cost(Air+Land) matching accounting lines & excel file -Yes/No',
'For Air - all ticket issued and matches all pax',
'For EXCH Air - passenger association correct & no dupe EXCH',
'Error - Yes/No',
'For EXCH Air - penalty check',
'Comments']
df = pd.concat([df, pd.DataFrame(columns=new_columns)])
I have a dataframe with 38 columns. I want to add 19 more blank columns at the end of it.
I’ve tried this:
df.insert([39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57],
['Quality Control - Dupe Ticketing/Dupe Invoicing',
'Error Type',
'Passed/Not Passed',
'Agent Name',
'Team Leader',
'Responsibility(GO/Market/Capability)',
'Remediation Comments',
'Financial Exposure/Dupe Value(USD)',
'Self Identified (Yes/No)',
'Remediation Date',
'Remediation Status (Yes/No)',
'Remarks',
'CM Impact Yes/No',
'Is product cost(Air+Land) matching accounting lines & excel file -Yes/No',
'For Air - all ticket issued and matches all pax',
'For EXCH Air - passenger association correct & no dupe EXCH',
'Error - Yes/No',
'For EXCH Air - penalty check',
'Comments'],
empty_value)
But this gives a TypeError: unhashable type: 'list'.
Insert does not allow for multiple columns. You can try it like this:
df = pd.DataFrame({'col1': [1,2,3], 'col2': ['a','b','c']})
newcols = ['col3', 'col4']
df[newcols] = None
print(df)
# col1 col2 col3 col4
# 0 1 a None None
# 1 2 b None None
# 2 3 c None None
You can add new empty columns doing this:
import pandas as pd
new_columns = ['Quality Control - Dupe Ticketing/Dupe Invoicing',
'Error Type',
'Passed/Not Passed',
'Agent Name',
'Team Leader',
'Responsibility(GO/Market/Capability)',
'Remediation Comments',
'Financial Exposure/Dupe Value(USD)',
'Self Identified (Yes/No)',
'Remediation Date',
'Remediation Status (Yes/No)',
'Remarks',
'CM Impact Yes/No',
'Is product cost(Air+Land) matching accounting lines & excel file -Yes/No',
'For Air - all ticket issued and matches all pax',
'For EXCH Air - passenger association correct & no dupe EXCH',
'Error - Yes/No',
'For EXCH Air - penalty check',
'Comments']
df.loc[:,new_columns] = pd.DataFrame()
Use pd.concat
:
cols = ['Quality Control - Dupe Ticketing/Dupe Invoicing',
'Error Type',
'Passed/Not Passed',
'Agent Name',
'Team Leader',
'Responsibility(GO/Market/Capability)',
'Remediation Comments',
'Financial Exposure/Dupe Value(USD)',
'Self Identified (Yes/No)',
'Remediation Date',
'Remediation Status (Yes/No)',
'Remarks',
'CM Impact Yes/No',
'Is product cost(Air+Land) matching accounting lines & excel file -Yes/No',
'For Air - all ticket issued and matches all pax',
'For EXCH Air - passenger association correct & no dupe EXCH',
'Error - Yes/No',
'For EXCH Air - penalty check',
'Comments']
df = pd.concat([df, pd.DataFrame(columns=new_columns)])