0

This is the start and returned values of the function I used to get some values:

def gal_uvw(distance=None, lsr=None, ra=None, dec=None, pmra=None, pmdec=None, vrad=None, plx=None):

    return (u,v,w)

I used column values of a dataframe(df) as variables of this function, and got a list of returned u,v and w values.

def gal_uvw(distance=None, lsr=None, ra=df['Col1'], dec=df['Col2'],
            pmra=df['Col3'], pmdec=df['Col4'], vrad=df['Col5'], plx=None)

Output:(1       0.076253
        2      43.303953
              ...
        506   -51.194802

        1      -8.192123
        2     -47.063398
              ...
        506   -22.488182)

How do I get these u,v and w lists of values and add them to the df I used in order to work the function? Each group of numbers would be a new column, so that I'd have:

df['U']=u
df['V']=v
df['W']=w
  • 1
    Hey are you trying to do something like this? [link](https://stackoverflow.com/questions/26869574/applying-a-python-function-that-returns-a-list-and-writing-to-columns-of-datafra) – PRIN Aug 26 '20 at 22:04
  • Yes! I tried several word combinations to find that question and did not find it. Thank you for pointing it out! – Augusto Baldo Aug 26 '20 at 22:20

1 Answers1

1

You should be able to assign it directly from unpacked variables as long as the length of u, v and w are the same as your dataframe.

import pandas as pd

def foo():
    return (0, 9, 8, 7, 6), (0, 91, 81, 71, 61), (0, 92, 82, 72, 62)

df = pd.DataFrame({'A': [1, 2, 3, 4, 5]})

u, v, w = foo()

df['u'] = u
df['v'] = v
df['w'] = w

print(df)

'''
Output:

   A  u   v   w
0  1  0   0   0
1  2  9  91  92
2  3  8  81  82
3  4  7  71  72
4  5  6  61  62

'''
M. Abreu
  • 366
  • 2
  • 5