When using np.genfromtxt
with usecols
specified by column names (e.g., ["B", "A"]
), the resulting data is incorrect—the values are correct, but the column names don’t match because genfromtxt
assigns names based on the order in the file, not the order in usecols
.
genfromtxt
reads columns in the file’s order and then applies names based on the usecols
list, causing mismatches if the column order is different.
Read all columns first without using usecols
, then extract only the desired columns by name:
data = np.genfromtxt(..., names=True, dtype=None)
new_data = np.core.records.fromarrays((data["B"], data["A"]), names="B,A")
This ensures the data and column names align correctly.