pythonpandasdataframe

How to slice column values in pandas DataFrame


I have a dataframe like this-

element      id  year  month days  tmax  tmin
      0 MX17004  2010      1   d1   NaN   NaN
      1 MX17004  2010      1  d10   NaN   NaN
      2 MX17004  2010      1  d11   NaN   NaN
      3 MX17004  2010      1  d12   NaN   NaN
      4 MX17004  2010      1  d13   NaN   NaN

where I want to further break days column like this

days
1
10
11
12
13

I have tried a couple of ways, but not successful in getting the output.


Solution

  • By using str slice

    df.days = df.days.str[1:]
    df
    
    Out[759]: 
       element       id  year  month days  tmax  tmin
    0        0  MX17004  2010      1    1   NaN   NaN
    1        1  MX17004  2010      1   10   NaN   NaN
    2        2  MX17004  2010      1   11   NaN   NaN
    3        3  MX17004  2010      1   12   NaN   NaN
    4        4  MX17004  2010      1   13   NaN   NaN
    

    User guide: Working with text data § Indexing with .str