Home > Blockchain >  why the columns read different in Pandas
why the columns read different in Pandas

Time:03-28

I have a csv file. when I run the command df.columns the column names read like the following.

Index(['sep=', 'Unnamed: 1'], dtype='object')

How to drop or change the column name to what I want.

The second row has the column name what I want.

CodePudding user response:

Maybe you are using pandas.read_csv to read the csv file, the header keyword parameter allows you to indicate a line in the file to use for header names (for ex. header = 1)

header : int, list of int, None, default ‘infer’
Row number(s) to use as the column names, and the start of the data.
Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None...

Or just skip the first line with skiprows (for ex. skiprows = 1)

skiprows : list-like, int or callable, optional Line numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file...

The names keyword parameter will be useful when you don't have the column name in the file.

names : array-like, optional
List of column names to use. If the file contains a header row, then you should explicitly pass header=0 to override the column names. Duplicates in this list are not allowed.

CodePudding user response:

column_names = ("Column1","Column2")
df.columns = column_names

to make the 2nd row of the dataset column names:

column_names = df.loc[1].values
df.columns = column_names
  • Related