Delete Untitled columns in pandas dataframe

I have a data file from AG columns, as shown below, but when I read it using pd.read_csv('data.csv') , it prints an extra unnamed column at the end for no reason.

 colA ColB colC colD colE colF colG Unnamed: 7 44 45 26 26 40 26 46 NaN 47 16 38 47 48 22 37 NaN 19 28 36 18 40 18 46 NaN 50 14 12 33 12 44 23 NaN 39 47 16 42 33 48 38 NaN 

I saw the data file once at different times, but I do not have additional data in any other column. How should I remove this extra column while reading? Thanks

+26
python pandas dataframe
source share
4 answers
 df = df.loc[:, ~df.columns.str.contains('^Unnamed')] In [162]: df Out[162]: colA ColB colC colD colE colF colG 0 44 45 26 26 40 26 46 1 47 16 38 47 48 22 37 2 19 28 36 18 40 18 46 3 50 14 12 33 12 44 23 4 39 47 16 42 33 48 38 

if the first column in the CSV file has index values, then you can do this instead:

 df = pd.read_csv('data.csv', index_col=0) 
+65
source share

First find the columns that have β€œnameless,” and then drop those columns. Note. You should also add inplace = True to the .drop options.

 df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True) 
+14
source share

The pandas.DataFrame.dropna function removes missing values (for example, NaN , NaT ).

For example, the following code will remove all columns from your data frame where all the elements of this column are missing.

 df.dropna(how='all', axis='columns') 
+3
source share

The approved solution does not work in my case, so my solution is as follows:

  ''' The column name in the example case is "Unnamed: 7" but it works with any other name ("Unnamed: 0" for example). ''' df.rename({"Unnamed: 7":"a"}, axis="columns", inplace=True) # Then, drop the column as usual. df.drop(["a"], axis=1, inplace=True) 

Hope this helps others.

+1
source share

All Articles