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

how to add a new column with different length into a existing dataframe

I am trying to go over a loop to add columns into a empty dataframe. each column might have a different length. Look like the final number of rows are defined by lenght of first column added. The columns with a Longer length will be cut values.

How to always keep all the values of each column when column length are different? Thanks

Here is case 1 with first column has lower length, then 2nd column’s value will be cut

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

import pandas as pd

df_profile=pd.DataFrame()
df_profile['A']=pd.Series([1,2,3,4])

df_profile['B']=pd.Series([10,20,30,40,50,60])

print(df_profile)

   A   B
0  1  10
1  2  20
2  3  30
3  4  40

Here are case 2 with first column has highest length, then it is find for other columns

import pandas as pd

df_profile=pd.DataFrame()
df_profile['A']=pd.Series([1,2,3,4,5,6,7,8])

df_profile['B']=pd.Series([10,20,30,40,50,60])

df_profile['C']=pd.Series([100,200,300,400,500,600])

df_profile['D']=pd.Series([100,200])

print(df_profile)

   A     B      C      D
0  1  10.0  100.0  100.0
1  2  20.0  200.0  200.0
2  3  30.0  300.0    NaN
3  4  40.0  400.0    NaN
4  5  50.0  500.0    NaN
5  6  60.0  600.0    NaN
6  7   NaN    NaN    NaN
7  8   NaN    NaN    NaN

>Solution :

You can use pd.concat to add another Series, e.g.:

# you have already this dataframe:
df_profile = pd.DataFrame()
df_profile["A"] = pd.Series([1, 2, 3, 4])

# you can use pd.concat to add another Series:
out = pd.concat([df_profile, pd.Series([10, 20, 30, 40, 50, 60], name="B")], axis=1)
print(out)

Prints:

     A   B
0  1.0  10
1  2.0  20
2  3.0  30
3  4.0  40
4  NaN  50
5  NaN  60
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