Home > Mobile >  How to specify header to a specific number of columns in csv and panda dataframe
How to specify header to a specific number of columns in csv and panda dataframe

Time:11-18

I have a csv file with 50 comma seperated values. for example, a row:

3290,171,12,134,23,1824,228,245,147,2999,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1

I want to specify headers to the 11 first columns in this csv file. I tried some ways but the data seems is corrupted.
What I did:

df = pd.read_csv("info/info.data", sep=',', header=None)
df_11 = df.iloc[:, 1:11]
df_11.columns = ['A', 'B', 'C', 'D', 'E', 'F' 'G', 'H', 'I', 'J', 'K']

What I'm doing wrong?

CodePudding user response:

assumption: you want to rename the first 11 columns

# new names for the columns
cols=['A', 'B', 'C', 'D', 'E', 'F' ,'G', 'H', 'I', 'J', 'K']

# using list comprehension, take value from cols for the first 11 columns and remainder
# keep as is
new_cols=[cols[c] if c < len(cols) else c for c in range(len(df.columns))    ]

df.columns=new_cols
df
A   B   C   D   E   F   G   H   I   J   ...     45  46  47  48  49  50  51  52  53  54
0   3290    171     12  134     23  1824    228     245     147     2999    ...     0   0   0   0   0   0   0   0   0   1

if you only need the first 11 columns and rename them


cols=['A', 'B', 'C', 'D', 'E', 'F' ,'G', 'H', 'I', 'J', 'K']

# filter columns as many as there are in the cols list
df2=df.iloc[:, :len(cols)]

# Rename columns replacing with cols
df2.columns= cols
df2

A   B   C   D   E   F   G   H   I   J   K
0   3290    171     12  134     23  1824    228     245     147     2999    1
  • Related