Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to delete column names from the middle of dataset

Hi i have a dataset in csv format. the issue with that data set is that it combine different csv files. and from the other csv files it also copied the column names. now i want to remove all column name which is in the middle of dataset

current csv file is like

col1  col2  col3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
col1  col2  col3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3
 1      2     3
col1  col2  col3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
col1  col2  col3
col1  col2  col3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3

want to change it to this column name only on the top

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

col1  col2  col3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3
 1      2     3 
 1      2     3

>Solution :

you can use this line of codes:

df = pd.read_csv(df_path)
# removing repeating headers
df = df[df.ne(df.columns).any(1)]

This solution compares each row with actual columns and works regardless of non-column rows being number or not

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading