Это логическая группировка по QID получение списка ответов с последующим разделением списка на столбцы
import re
data = """ QID Category Text QType Question: Answer0 Answer1 Country
0 16 Automotive Access to car Single Do you have access to a car? I own a car/cars I own a car/cars UK
1 16 Automotive Access to car Single Do you have access to a car? I lease/ have a company car I lease/have a company car UK
2 16 Automotive Access to car Single Do you have access to a car? I have access to a car/cars I have access to a car/cars UK
3 16 Automotive Access to car Single Do you have access to a car? No, I don’t have access to a car/cars No, I don't have access to a car UK
4 16 Automotive Access to car Single Do you have access to a car? Prefer not to say Prefer not to say UK"""
a = [[t.strip() for t in re.split(" ",l) if t!=""] for l in [re.sub("([0-9]?[ ])*(.*)", r"\2", l) for l in data.split("\n")]]
df = pd.DataFrame(data=a[1:], columns=a[0])
# lazy - want first of all attributes except QID and Answer columns
agg = {col:"first" for col in list(df.columns) if col!="QID" and "Answer" not in col}
# get a list of all answers in Answer0 for a QID
agg = {**agg, **{"Answer0":lambda s: list(s)}}
# helper function for row call. not needed but makes more readable
def ans(r, i):
return "" if i>=len(r["AnswerT"]) else r["AnswerT"][i]
# split list from aggregation back out into columns using assign
# rename Answer0 to AnserT from aggregation so that it can be referred to.
# AnswerT drop it when don't want it any more
dfgrouped = df.groupby("QID").agg(agg).reset_index().rename(columns={"Answer0":"AnswerT"}).assign(
Answer0=lambda dfa: dfa.apply(lambda r: ans(r, 0), axis=1),
Answer1=lambda dfa: dfa.apply(lambda r: ans(r, 1), axis=1),
Answer2=lambda dfa: dfa.apply(lambda r: ans(r, 2), axis=1),
Answer3=lambda dfa: dfa.apply(lambda r: ans(r, 3), axis=1),
Answer4=lambda dfa: dfa.apply(lambda r: ans(r, 4), axis=1),
Answer5=lambda dfa: dfa.apply(lambda r: ans(r, 5), axis=1),
Answer6=lambda dfa: dfa.apply(lambda r: ans(r, 6), axis=1),
).drop("AnswerT", axis=1)
print(dfgrouped.to_string(index=False))
вывод
QID Category Text QType Question: Country Answer0 Answer1 Answer2 Answer3 Answer4 Answer5 Answer6
16 Automotive Access to car Single Do you have access to a car? UK I own a car/cars I lease/ have a company car I have access to a car/cars No, I don’t have access to a car/cars Prefer not to say
больше динамики c
Это немного расширяет python
. Использование **kwargs
и functools.partial
. На самом деле это все еще стат c, столбцы определены как константа MAXANS
import functools
MAXANS=8
def ansassign(dfa, row=0):
return dfa.apply(lambda r: "" if row>=len(r["AnswerT"]) else r["AnswerT"][row], axis=1)
dfgrouped = df.groupby("QID").agg(agg).reset_index().rename(columns={"Answer0":"AnswerT"}).assign(
**{f"Answer{i}":functools.partial(ansassign, row=i) for i in range(MAXANS)}
).drop("AnswerT", axis=1)