0

I have a problem with the extraction Given is a column Date, where I want to extract the months for example: 2022-05-12 --> 05

How can I programm this?

import pandas as pd
import re
DF = pd.DataFrame({
    "item_id": [1, 2, 3, 4],
    "costumer": ["CG", "MD", "CC", "CZ"],
    "Betrag": [150, 12, 78, 56],
    "games": [["Minecraft", "Uno"], ["WOW", "Minecraft"], ["WOW", "Minecraft"], ["WOW", "Minecraft", "The last of us"]], 
    "date": ["2022-11-18", "2022-09-12", "2022-08-26", "2022-05-12"]
})

DF['date'] = [re.findall("(-)(-)", i) for i in DF['date']]
DF

Expected Output: each row in date: 11 09 08 05

cs95
  • 379,657
  • 97
  • 704
  • 746
CGD 16
  • 38
  • 5
  • 1
    The pattern `"(-)(-)"` will find two dashes next to each other. None of your date values match that pattern. Your date values have two dashes **with numbers in between**, which that pattern does not match. – John Gordon Apr 15 '23 at 16:36
  • 1
    your regex is incorrect yes, but why aren't you using to_datetime and extracting the month using `dt.month`? – cs95 Apr 15 '23 at 16:38
  • `DF['date'] = [d.split('-')[1] for d in DF['date']]` should work. – René Apr 15 '23 at 17:04
  • @cs95 - thanks, I didn't know, that this function exists :) – CGD 16 Apr 15 '23 at 18:23
  • @JohnGordon I'm curious what the solution might be. Could you please write the solution down? – CGD 16 Apr 15 '23 at 18:27
  • It's also working: `DF["month"] = pd.datetime(DF["date"]).dt.month` – CGD 16 Apr 15 '23 at 20:21

0 Answers0