Skip to main content
  1. About
  2. For Teams
Asked
Modified 3 months ago
Viewed 27k times
5

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.

2 Answers 2

20

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

Sign up to request clarification or add additional context in comments.

Comments

0

Use extract with regex:

df['days'] = df.days.str.extract('d(\d+)', expand=False)
print(df)

Output:

   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

Comments

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.