-1

I'm very very new in python and pandas so my question is very basic.

I have a simple dataframe that has its index and a column 'years':

    Years = pd.DataFrame({'Years': range(1900,2000,1)})

To this dataframe, I need to add a column that for each year it performs a specific calculation, say: Year i = X*i

The year (i.e. 1900, 1991, etc.) doesn't matter as such, more that each "i" belongs to a specific year.

I hope you can help me resolve this. Thansks very much.

Yannis
  • 1
  • 1
  • 1

2 Answers2

1

Does this solve your problem?

Years = pd.DataFrame({'Years': range(1900,2000,1)})
Years['calculation'] = 0

for row in Years.index:
    Years['calculation'][row] = 10**row

You can also specifically use the value in the row before, e.g. like

for row in Years.index:
    Years['calculation'][row] = 10 * (row - 1)
Kora K
  • 425
  • 1
  • 4
  • 15
  • What should the new column look like? – Kora K Feb 28 '17 at 16:01
  • I'm sorry if I confused you. I'm familiarising myself with this, and can't seem to find any solution online. As I do not know the jargon, i'll be more descriptive. Each value in each row of the new column uses the result of the previous row. So if the first row is (1900, 10), the second should be (1901,10^1), the third (1902, 10^2) and so forth. (Need to learn how to post with the right style in here too...) – Yannis Feb 28 '17 at 16:13
  • I edited my answer - I'm not sure if this is the most elegant solution, but it works. ;) – Kora K Feb 28 '17 at 16:35
  • Yes! Yes it does! Many thanks to begin with. I assume, however, that this only works if the index is automatically set from 0 onwards. If you have a moment and you know the answer, would you mind letting me know how this could work if the index was something else (e.g. date, string). – Yannis Feb 28 '17 at 16:44
  • It also gives this message: 12: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame – Yannis Feb 28 '17 at 16:50
  • No it doesn't. So the calculation and filling in the column works, but still with this warning. – Yannis Feb 28 '17 at 17:23
  • This should take care of the warning: `Years.loc[row, 'calculation'] = 10 * (row - 1)`. – Kora K Feb 28 '17 at 19:05
  • As to your other question, this seems to be a better general solution to use a previous rows' value: http://stackoverflow.com/questions/22081878/get-previous-rows-value-and-calculate-new-column-pandas-python – Kora K Feb 28 '17 at 19:09
  • Thank you so much for your time! I'll try these out to see what fits best! But your's works for sure. – Yannis Feb 28 '17 at 20:51
0
df = pd.DataFrame({'Years': range(1900,2000,1)})

df.head()

enter image description here

to do calculations all you have to do is this:

df['new_column'] = df['Years'] * 3

df.head()

enter image description here

Vikash Singh
  • 13,213
  • 8
  • 40
  • 70