1

I have a dataframe and two array with differents sizes and i want to create a single dataframe

for example

import pandas
import numpy

 df = pandas.DataFrame(numpy.array([[0,0,1]]),columns = ['A', 'B', 'C'])
  V1=numpy.array([0,1,3,4])
  V2=numpy.array([2,3,5,8,11,12])

I want obtain a single dataframe like :

  A  B  C V1 V2
  0  0  1 0   2
  0  0  1 1   3
  0  0  1 3   5
  0  0  1 4   8   
  0  0  1 Nan 11
  0  0  1 Nan 12
1

1 Answer 1

1

First is necessary repeat array in first DataFrame by maximum length of array, then create for each array Series and join together by concat:

a = [V1, V2]
n = max(map(len, a))
df1 = pd.DataFrame(np.array(np.repeat([[0,0,1]], n, axis=0)), columns = ['A', 'B', 'C'])
df = pd.concat([df1, pd.Series(V1, name='V1'), pd.Series(V2, name='V2')], axis=1)
print (df)
   A  B  C   V1  V2
0  0  0  1  0.0   2
1  0  0  1  1.0   3
2  0  0  1  3.0   5
3  0  0  1  4.0   8
4  0  0  1  NaN  11
5  0  0  1  NaN  12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.