-2

Here's how my file looks like

Name Gender Birth Death
John Male 1940 1990
Steve Male 1953 1985
John Male 1933 1965
Sara Female 1917 1955

I have tried these methods and getting some errors.

file = pd.read_csv('file')

df.insert(6, "Age", file['Death - Birth'])
age = pd.Series([])

and

for i in range(len(df)):
age[i] = df['Death Year - Birth Year']

I want to add a age column which would calculate the age based on birth and death year.

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
Afroboy23
  • 31
  • 9
  • 1
    What errors are you getting? Please include **all** relevant code and data, as well as an explanation of your debugging efforts. See: [mcve], https://meta.stackoverflow.com/q/261592/11301900, https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – AMC Dec 14 '19 at 06:08
  • 1
    I would recommend reading the Pandas docs, your code is unidiomatic and could be simplified drastically thanks to some knowledge of the library. – AMC Dec 14 '19 at 06:09

1 Answers1

0

The following should be a simple fix:

df = pd.read_csv('file.csv')

df['age'] = df['death'] - df['birth']
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
David W
  • 66
  • 5