0

I have a dataset like below:

data
id    message
 1    ffjffjf
 2    ddjbgg
 3    vvnvvv
 4    eevvff
 5    ggvfgg

Expected output:

data
id    message  splitmessage
 1    ffjffjf    ff
 2    ddjbgg     dd
 3    vvnvvv     vv
 4    eevvff     ee
 5    ggvfgg     gg

I am very new to Python. So how can I take 1st two letters from each row in splitmessage variable.

my data exactly looks like below image
need to split that
so from the image i want only hour and min's which are 12 to 16 elements in each row of vfreceiveddate variable.

surendra
  • 1,051
  • 3
  • 10
  • 11
  • Show your current code. Also, you do want to do your basic research based on the terms "python substring" – reto Mar 30 '15 at 06:20

3 Answers3

1
dataset = [
    { "id": 1, "message": "ffjffjf" },
    { "id": 2, "message": "ddjbgg" },
    { "id": 3, "message": "vvnvvv" },
    { "id": 4, "message": "eevvff" },
    { "id": 5, "message": "ggvfgg" }
]

for d in dataset:
    d["splitmessage"] = d["message"][:2]
Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
  • my data type is pandas.core.series.series so how can i do that at that point – surendra Mar 30 '15 at 08:14
  • I asked exactly for output of ```print``` command, which will help me to determine what data structure is used to represent the date you have to deal with. – Ihor Pomaranskyy Mar 30 '15 at 08:36
  • By the way, if you have to extract some parts of date/time representation, it's probable not the best idea to manipulate strings. You can work with ```datetime``` objects. – Ihor Pomaranskyy Mar 30 '15 at 08:38
0

What you want is a substring.

mystr = str(id1)
splitmessage = mystr[:2]

Method we are using is called slicing in python, works for more than strings.

In next example 'message' and 'splitmessage' are lists.

message = ['ffjffjf', 'ddjbgg']
splitmessage = []
for myString in message:
    splitmessage.append(myString[:2])
print splitmessage
Community
  • 1
  • 1
Laraconda
  • 667
  • 6
  • 15
0

If you want to do it for the entire table, I would do it like this:

dataset = [id1, id2, id3 ... ]
for id in dataset:
   splitid = id[:2]
Seraphim
  • 181
  • 8