为何Pandas读取CSV后DataFrame索引从第二行开始?求解决
Hey there! Let's break down what's going on here and fix that index issue quickly.
The Root Cause
When you saved your DataFrame to CSV using results.to_csv(..., header=None), you told pandas not to write column headers into the file. But when you read the CSV back with plain pd.read_csv(), pandas defaults to treating the first line of the file as the column headers.
That means your first actual data row (ROCO_CLEF_TEST_00001 ...) got turned into the column names of your new DataFrame, and your real data starts from the second line—so the index looks like it's starting from the second row.
The Simple Fix
Just add the header=None parameter when reading the CSV, matching what you did when writing it. This tells pandas the file has no header row, so it'll treat every line as data and assign the default index starting at 0 to your first data row.
Here's the corrected reading code:
kf = pd.read_csv('/home/udas/scratch/projekt01/results.csv', header=None) print(kf.head())
Optional: Add Meaningful Column Names
If you want to reassign the original column names to your DataFrame while reading, use the names parameter:
kf = pd.read_csv( '/home/udas/scratch/projekt01/results.csv', header=None, names=["Picture", "predictions"] ) print(kf.head())
This gives you both the correct index mapping and properly labeled columns.
内容的提问来源于stack exchange,提问作者user3156370

