Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas : Add column to another column

I’m confused on how do I add a column to another in pandas

Here is what I’m trying to do :

from pandas import DataFrame
df1 = DataFrame({'a':[1,2], 'b':[3,4]})
concat((df1['a'], df1['b'].rename({'b':'a'}))).reset_index(drop=True)

Which do return what I want : A serie with my 4 values. What I don’t understand is : Why I can’t assign it to column ‘a’ ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> from pandas import DataFrame
>>> df1 = DataFrame({'a':[1,2], 'b':[3,4]})
>>> concat((df1['a'], df1['b'].rename({'b':'a'}))).reset_index(drop=True)
0    1
1    2
2    3
3    4
dtype: int64
>>> df1['a'] = concat((df1['a'], df1['b'].rename({'b':'a'}))).reset_index(drop=True)
>>> df1
   a  b
0  1  3
1  2  4

Is there any way to make it more readable by the way? I’m confused on how it should worked… Note that I don’t need column ‘b’ afterward.

Thanks for your help 🙂

Sam

>Solution :

pandas series don have columns.

if you want use column by Dataframe, use df[['a']] instead df['a']

& you want change column’s name need axis or columns

pd.concat([df1[['a']], df1[['b']].rename(columns={'b':'a'})]).reset_index(drop=True)

output

    a
0   1
1   2
2   3
3   4

If i create your output using your code, code like above. But I wouldn’t use the above code.

i will use following code:

pd.concat([df1['a'], df1['b']]).to_frame('a').reset_index(drop='True')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading