I have two files. One contains the metadata/labels, the other contains the actual count data that has a label corresponding to the metadata file. I went through the metadata file and slices out the labels I wanted using Pandas and exported it into a list.
How can I take that list of labels and use that to slice a Pandas DataFrame by column label?
I've done something similar with row labels, but that was using Pandas .isin() function, which can't be used on columns.
Edit: When I'm slicing out rows based on whether the name of the row is found in a list I use a one-liner similar to this
row_list = ['row_name1', 'row_name2', row_name3']
sliced_rows = df[df['row_names'].isin(row_list)]
df =
row_names 1 2 3 4
row_name1 0 2 0 6
row_name5 0 0 1 0
row_name2 0 0 0 0
row_name17 0 5 6 5
So here I'd get row_names1 & rownames_2
I'm trying to do the same thing, but when row_names are labelling the columns instead of the names.
So the matrix would look something like this.
label column_name1 column_name2 column_name3 column_name4
1 0 2 0 6
2 0 0 1 0
3 0 0 0 0
4 0 5 6 5`
And I'd select by column based on whether or not the name of that column was in a list for the entire dataframe.
col_list = [col for col in df if col in other_col_list]?