1

I have a pandas DataFrame set up like this:

 Type     time
 A        0
 A        1
 A        2
 B        0
 B        1
 B        2

I need to generate a list (or a Series) structured as follows:

["A.1-A.0", "A.2-A.0", "B.1"-"B.0", "B.2"-"B.0"]

Would groupby or similar functions would be able to generate such a list (or Series)?

1 Answer 1

2
import pandas as pd
from StringIO import StringIO

data =  StringIO("""Type     time
A        0
A        1
A        2
B        10
B        11
B        12""")
df = pd.read_csv(data, delim_whitespace=True, dtype="O")

def set_first(x):
    x["ptime"] = x.time.values[0]
    x = x[1:]
    r = x.Type + "." + x.time + "-" + x.Type + "." + x.ptime
    return r

print df.groupby(df.Type, group_keys=False).apply(set_first)

output:

1      A.1-A.0
2      A.2-A.0
4    B.11-B.10
5    B.12-B.10
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, ended up implementing something very similar to this.

Your Answer

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.