Home > Mobile >  How to read csv file having columns matching with list of similar columns names (not exact column na
How to read csv file having columns matching with list of similar columns names (not exact column na

Time:09-17

Suppose I have a csv file with columns as "A__123","B__324","C__599","D__232","E__235".

columns_to_read = ["A","C","E"]

Now I want to read only columns in list columns_to_read

How can I read the csv based on the above condition.

I know I can use usecols to read the specific columns of csv but how can I read similar columns?

CodePudding user response:

I don't include the reading from the csv, but i will do it like this, when you get lists as return values from reading the .csv file

test_data = ["A_123", "B_324", "C_599", "D_232", "E_235"]

columns_to_read = ["A", "C", "E"]

for i in range(len(test_data)):
    for j in range(len(columns_to_read)):
        if (columns_to_read[j] in test_data[i]):
            print(test_data[i])
    

CodePudding user response:

You can do this.

`df = read_csv("file.csv")
col_name1 = []
col_name2 = []
for i in range(1,len(df)):
    col_name1.append(df[i][0])#0 is the index of the column.
    col_name2.append(df[i][1])
`

and then merge the code in one dataframe like so

df2 = pd.DataFrame(list(zip(col_name1,col_name2)),columns=['Column_name1','Column_name2])

refer to https://www.geeksforgeeks.org/zip-in-python/ for zip() function. refer to https://www.programiz.com/python-programming/methods/built-in/list for list() function. pd.DataFrame() is used to create dataframe.

  • Related