Reputation: 63
I've got a huge dataframe physioDf and want to copy some of the columns into a different df called smallPysioDf. I know, that I can copy individual columns like this:
smallPhysioDf['column_name'] = physioDf['column_name'].values
But I now want to copy 30 columns belonging to the same Variable. Each column names starts the same (e.g. "VariableName_") but end with a specific number from 1 to 30. What would be the fastest way to copy all of those columns into smallPhysioDf? I believe I would have to use a for loop but I am not sure how. Very happy for any help.
Upvotes: 0
Views: 783
Reputation: 4680
It will be quickest to select all columns at once:
columns = [f"VariableName_{i}" for i in range(1, 31, 1)]
smallPhysioDf = physioDf[columns].copy()
If smallPhysioDf
already exists you can instead append (if the VariableName
columns are already in the DataFrame), or merge (if the VariableName
columns are new).
Upvotes: 1
Reputation: 150785
You can use filter
to extract the column names:
for c in physioDf.filter(regex='^VariableName_').columns:
smallPhysioDf[c] = physioDf[c]
Or you can use f-string:
for n in range(1,31):
col_name = f'VariableName_{n}'
smallPhysioDf[col_name] = physioDf[col_name]
Upvotes: 0