I have a big csv file, nearly 400 columns I want to read only columns starting with 'A' and with 'X'. For example, I have the following columns:
- A_1
- A_2
- Q_1
- Q_2
- D_1
- X_1
- X_2
When I read from the csv file with using pandas, I want to select only:
- A_1
- A_2
- X_1
- X_2
And I don't know how many As and Xs I have.
CodePudding user response:
You read the file twice: once for the headers only and once for the actual data:
df = pd.read_csv('data.csv', usecols=lambda col: col.startswith('A_') or col.startswith('X_'))