0

I have a data set from a csv file and I would like to iterate the data based on a string value within a column. I would like to only use data if a certain column has say year 2014 for instance.

df = pd.read_csv("data.csv")
year = df['Year']

while year == '2014':
    ## do something

My csv file has 3 columns

Year | Mileage | Price 
2014    35000    15000

What would be the correct way to do this?

Ryan D
  • 741
  • 1
  • 11
  • 29
  • `df2014 = df[df['Year']==2014]`? If you want to manipulate the data, better use `df2014 = df[...].copy()`. – Quang Hoang Nov 25 '19 at 21:02
  • What Quang Hoang said, if your `Year` values are strings use `df2014 = df[df['Year']=='2014'].copy()` – Erfan Nov 25 '19 at 21:09
  • Ok thanks, how would that be done in a while loop then? – Ryan D Nov 25 '19 at 21:12
  • What can be done in a `while` loop? `df2014` gives you all 2014 data. – Quang Hoang Nov 25 '19 at 21:16
  • Ah ok I got you, thanks! Can you add as answer so I can check it off – Ryan D Nov 25 '19 at 21:20
  • When working with data and dataframes, you have to let go of `loops` unless you have a really complex data manipulation request. `Pandas` and `numpy` cover most of our needs with their methods. Learn a bit more of these modules if you want to work with data @RyanD – Erfan Nov 25 '19 at 21:30
  • Thanks @Erfan makes sense I am just used to PHP, still confused on how I would use the data set once its filtered. instead of df['Mileage'] would I do df2014['Mileage']? – Ryan D Nov 25 '19 at 21:46
  • Read the link of the duplicated question, these are really basic data wrangling methods. Spend some time in the pandas documntation as well. – Erfan Nov 25 '19 at 22:04

1 Answers1

-2

Does this work?

df['res'] = df['Year'].map(lambda x: x+1000 if x == '2014' else pass)

For more complex stuff:

New_col = []

for i in range(len(df)):
    if df.loc['Year',i] == '2014':
        # Do
        New_col.append(RESULT)
    else:
        pass

df['RESULT'] = New_col
J.Doe
  • 224
  • 1
  • 4
  • 19
  • Not sure whats going on here could you explain it a little? Whats the use of lambda here? – Ryan D Nov 25 '19 at 21:06
  • ```map(Iterable, func)``` is a python builtin function, which applies another function (func) to every element of an iterable object (Iterable). Pandas has it's own ```.map()``` method, which works the same way, just on columns and written as ```df['col'].map()```. Lambda is a neat little function, which works similar to a list comprehension. "For x: do x+1000 if x=='2014' else do nothing." Lambda functions always need an else statement, if you are using it with conditionals. Also, it can't do elif. The second example is the more general one. Lambda is useful as in quick and dirty. – J.Doe Nov 25 '19 at 21:12