Skip to content
Advertisement

Manipulate string to drop columns on pandas

I’m trying to manipulate a list (type: string) to use that list to drop some columns from a dataframe. Dataframe

The list is from a dataframe that I created a condition to return columns whose sums of all values ​​are zero:

Selecting the columns with sum = 0

condicao_CO8 = (ex8_centro_oeste.sum(axis = 0) == 0)
condicao_CO8 = condicao_CO8[condicao_CO8 == True]
condicao_CO8.to_csv('D:ProgramasCO_8.csv')

Importing the dataframe and turning it into a list:

CO8 = pd.read_csv(r'D:ProgramasCO_8.csv',
                 delimiter=','
                 )
CO8.rename(columns={'Unnamed: 0': 'Nulos'}, inplace = True)
CO8.rename(columns={'0': 'Info'}, inplace = True)
CO8.drop(columns = ['Info'], inplace = True)
CO8.columns          

Images from the list: List

Some itens from the list:

0                                          ABI - LETRAS
1                                 ADMINISTRAÇÃO PÚBLICA
2                                             AGRONOMIA
3                                             ALIMENTOS
4                                          ARQUIVOLOGIA
5                                  AUTOMAÇÃO INDUSTRIAL
6                                           BIOMEDICINA
7                                         BIOTECNOLOGIA
8                                   CIÊNCIAS - BIOLOGIA
9                                    CIÊNCIAS - QUÍMICA
10                                    CIÊNCIAS AGRÁRIAS
11                                  CIÊNCIAS BIOLÓGICAS
12                    CIÊNCIAS BIOLÓGICAS E CONSERVAÇÃO
13                               CIÊNCIAS DA COMPUTAÇÃO
14                                  CIÊNCIAS ECONÔMICAS 

My goal is to transform the list so that I can drop the columns from that.

Transforming this list into this: "ABI - LETRAS", "ADMINISTRAÇÃO PÚBLICA", "AGRONOMIA", "ALIMENTOS", "ARQUIVOLOGIA", "AUTOMAÇÃO INDUSTRIAL"...

for this I made the following code (unsuccessful)

list_CO8 = ('"," '.join(CO8['Nulos'].apply(str.upper).tolist()))

Please, can anyone help me?

I’m trying to get:

list = "ABI - LETRAS", "ADMINISTRAÇÃO PÚBLICA", "AGRONOMIA", "ALIMENTOS", "ARQUIVOLOGIA", "AUTOMAÇÃO INDUSTRIAL"...

To make: ex8_centro_oeste.drop(columns=[list])

Dataset link: Link for Drive (8kb)

Advertisement

Answer

I read your question properly only now. I deleted my previous answer. You want to drop all the columns that sums to zero, if I am not mistaken.

import pandas as pd
data = pd.read_csv("./centro-oeste.csv")

import numpy as np
data.columns[np.sum(data) == 0] # data is the dataframe
# It out puts a list that you can use.
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement