8

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.

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks. I believe this will do.
How to delete perticular value from dataframe. Like df['2'].astype('str')=='23' and the rest column's value shift up.
@Leo - Do you think boolean indexing by df[df['2'].astype('str')=='23'] ?
@Leo - Is possible use df['2'] = pd.Series(df['2'].replace(23, np.nan).dropna().values) ?
@panda - use df.loc[550, 2] = 'your value'
|
0

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

1 Comment

Please provide a little explanation of your code. Code dumps like this are rarely helpful for future visitors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.