1

I'm currently using panda for data science purposes, which is very unoriginal, so I'm sure there's a better way than mine of adding columns to a dataframe calculated from the others, in an excel fashion.

For example, I've got a dataframe in which I've got a mass and a period, and I'd like to add a column with (M/P)^(1/3), M and P being converted in the appropriate units. I then write

day_sec=60.0*60.0*24.0
Msun=1.989 * 10**30
clean=reduced.dropna()
v=pd.DataFrame(clean['orbital_period'].apply(lambda x: x**(1/3.0))/clean['star_mass'].apply(lambda x: x**(1/3.0)), columns=list('v'))*day_sec/(Msun**(1/3.0))
clean.append(v)

Which doesn't even act like I want, and is very complicated both to write and read (here, the equation was very very simple). Any suggestion?

1
  • If you want to create a new column from existing columns, the best way to do it is something like df['c'] = df['a'] + df['b']. Commented Dec 20, 2016 at 17:16

1 Answer 1

5

You should not use apply at all here. You should write the new column as one vectorized operation that looks something like this.

clean['v'] = clean['orbital_period'] ** (1/3.0) / clean['star_mass'] ** (1/3.0) *day_sec/(Msun**(1/3.0))

Which looks like it can be reduced to the following:

power = 1/3.0
(clean.orbital_period / (clean.star_mass * Msun)) ** power * day_sec
Sign up to request clarification or add additional context in comments.

2 Comments

Or with pow, something like pow(clean.orbital_period / (clean.star_mass * Msun), 1/3.0) * day_sec should work.
When I run your code, I get : /Users/Home/anaconda/lib/python2.7/site-packages/ipykernel/__main__.py:5: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

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.