I'm enquiring about efficiently assigning column headers to CSV files with a comma delimiter. At the moment time I'm manually assigning the headers once I know how many columns there are. The problem is, the number of columns varies with different files.
So the first Dataframe below has 3 columns. Which I assign via the following.
import pandas as pd
d = ({
'Col 1' : ['X','Y'],
'Col 2' : ['A','B'],
'Col 3' : ['C','D'],
})
df = pd.DataFrame(data=d)
df.columns = ['A','B','C']
If I have the following df and I use the same code it will return an error.
ValueError: Length mismatch: Expected axis has 2 elements, new values have 3 elements
d = ({
'Col 1' : ['X','Y'],
'Col 2' : ['A','B'],
})
df = pd.DataFrame(data=d)
df.columns = ['A','B','C']
I understand this is because there are only 2 columns. I'm asking about efficiently assigning headers A-n.
I know it's not not hard to alter df.columns to ['A','B'] but if I'm doing this multiple times a day it becomes very inefficient.
d = {'A':[...], 'B':[...]}. By the way, after you edited you dictionary, it has only one value now (bvecause all three keys are identical).DataFramefrom what you've written, but in the second scenario, you're trying to assign a third column name to aDataFramewith only 2 columns.A-n. Withnbeing the final header. At the moment I'm manually counting this.