Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zero index for all rows in python dataframe

I have problem with indexing python dataframe. I have dataframe which I fill it with loop. I simplified it like this :

d = pd.DataFrame(columns=['img', 'time', 'key'])
for i in range(5):
    image = i
    timepoint = i+1
    key = i+2
    temp = pd.DataFrame({'img':[image], 'timepoint':[timepoint], 'key': [key]})
    d = pd.concat([d, temp])

The problem is since it shows 0 as and index for all rows, I can not access to the specific row based on .loc[]. Does anybody have any idea how can I fix the problem and get normal index column?

like image 661
ga97rasl Avatar asked Aug 28 '26 20:08

ga97rasl


1 Answers

You may want to use the ignore_index parameter in your concatenation :

d = pd.concat([d, temp], ignore_index=True)

This gives me the following result :

   img  key time  timepoint
0  0.0  2.0  NaN        1.0
1  1.0  3.0  NaN        2.0
2  2.0  4.0  NaN        3.0
3  3.0  5.0  NaN        4.0
4  4.0  6.0  NaN        5.0
like image 109
3kt Avatar answered Aug 30 '26 11:08

3kt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!