question.it
question.it

Reputation: 2968

How to get python dataframe column name using filter

I have imported one csv file to data frame & it has some 250+ columns and last column name starts with 'Unnamed:***' & some digit attached to it like "Unnamed: 1272"

I want to get that column name which starts 'Unnamed'. Below script didn't help.

dfColumns = pd.DataFrame(data.columns, columns=['columnName'])
UnnamedColumnName = str(dfColumns.loc[dfColumns['columnName'].str.contains('Unnamed')])

Result: ' columnName\n1272 Unnamed: 1272'

Below script also tried but no use:

data.columns.str.contains('Unnamed')

Expected results in 'Unnamed: 1272' in string variable "UnnamedColumnName", I want use this variable in delete columns script.

Upvotes: 1

Views: 187

Answers (1)

EdChum
EdChum

Reputation: 394031

If it's always the last column you can just do

last_col = df.columns[-1]

You can also rename this using rename:

df = df.rename(columns={'new_name':df.columns[-1]})

Also str.contains returns you a boolean mask of the columns that match the string, you need to use this mask against the columns array:

data.columns[data.columns.str.contains('Unnamed')]

will return you an array with all columns where the boolean condition is met

Upvotes: 2

Related Questions