Home > Software engineering >  Build a 1x10 dataframe and fill it with a row vector
Build a 1x10 dataframe and fill it with a row vector

Time:11-08

I have a 1x10 dataframe where the name of each column is a string in a list. I have a 1x10 row vector with values. I would like to integrate this vector in the dataframe, so that I have the names in the list as column names, and the values of the vector in a single row.

How could I do that ? The only way I found was appending eveything into one column, and rename the index with the names in my list, but I want a 1x10 dataframe instead of a 10x1.

names = ['agmi',  'aglo', 'mwmi', 'mcha', 'mcmi', 'mclo', 'bkdr', 'mchi', 'melo', 'mwlo']

alt_GPS = np.array([[ 0.        ,  0.        ,  0.85253906,  6.12797546,  5.49960327,
        11.00892639,  0.        ,  2.08251953,  0.        ,  2.4508667 ]])

alt = pd.DataFrame(columns=names)

CodePudding user response:

Your alt_GPS array is already 2D, so isn't simply this what you want:

alt = pd.DataFrame(alt_GPS, columns=names)

Output:

   agmi  aglo      mwmi      mcha      mcmi       mclo  bkdr     mchi  melo      mwlo
0   0.0   0.0  0.852539  6.127975  5.499603  11.008926   0.0  2.08252   0.0  2.450867

If you want to create the dataframe first and add afterwards:

alt = pd.DataFrame(columns=names)
alt.loc[0] = alt_GPS[0]
  • Related