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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With