Hayat
Hayat

Reputation: 1639

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.

Upvotes: 8

Views: 21087

Answers (2)

woodword
woodword

Reputation: 21

If that 2x3 dataframe is your original dataframe, you can add an extra row to dataframe by pandas.concat().

For example:

pandas.concat([your_original_dataframe, pandas.DataFrame([[181]] , columns=[2] )], axis=0)

This will add 181 at the bottom of column 2

Upvotes: 0

jezrael
jezrael

Reputation: 862451

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

Upvotes: 6

Related Questions