Sunday, 21 October 2018

Create a column that has the same length of the longest column in the data at the same time

I have the following data:

data = [[1,2,3], [1,2,3,4,5], [1,2,3,4,5,6,7]]
dataFrame = pandas.DataFrame(data).transpose()

Output:

     0    1    2
0  1.0  1.0  1.0
1  2.0  2.0  2.0
2  3.0  3.0  3.0
3  NaN  4.0  4.0
4  NaN  5.0  5.0
5  NaN  NaN  6.0
6  NaN  NaN  7.0

Is it possible to create a 4th column AT THE SAME TIME the others columns are created in data, which has the same length as the longest column of this dataframe (3rd one)?

The data of this column doesn't matter. Assume it's 8. So this is the desired output can be:

     0    1    2    3
0  1.0  1.0  1.0  8.0
1  2.0  2.0  2.0  8.0
2  3.0  3.0  3.0  8.0
3  NaN  4.0  4.0  8.0
4  NaN  5.0  5.0  8.0
5  NaN  NaN  6.0  8.0
6  NaN  NaN  7.0  8.0

In my script the dataframe keeps changing every time. This means the longest columns keeps changing with it.

Thanks for reading



from Create a column that has the same length of the longest column in the data at the same time

No comments:

Post a Comment