Extract specific columns from CSV files using column indices
Cut Columns
Cut Columns extracts one or more specific columns from a CSV file using their column index. It is a lightweight way to reduce large tabular datasets and focus only on the variables required for downstream analysis.
Extract the second column from a CSV file:
cut -d',' -f2 input.csv > column2.csv
Extract columns 1 and 3:
cut -d',' -f1,3 input.csv > selected.csv
import pandas as pd
df = pd.read_csv("input.csv")
df.iloc[:, [0]].to_csv(
"column1.csv",
index=False
)
cut convention and starts from
1 rather than 0.
For example:
1 refers to the first column,
2 refers to the second column, and so on.