Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Value to a dataframe column

I have a pandas dataframe

0      1      2    3
0  173.0  147.0  161  162.0
1    NaN    NaN   23    NaN

I just want to add value a column such as

           3
       0 161
       1  23
       2 181

But can't go with the approch of loc and iloc. Because the file can contain columns of any length and I will not know loc and iloc. Hence Just want to add value to a column. Thanks in advance.

like image 982
Hayat Avatar asked Jun 27 '26 18:06

Hayat


1 Answers

I believe need setting with enlargement:

df.loc[len(df.index), 2] = 181
print (df)
       0      1      2      3
0  173.0  147.0  161.0  162.0
1    NaN    NaN   23.0    NaN
2    NaN    NaN  181.0    NaN
like image 59
jezrael Avatar answered Jun 30 '26 08:06

jezrael



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!