【发布时间】:2020-08-04 09:50:30
【问题描述】:
我有一个数据框,并且想创建另一个列,该列将名称以相同 value 开头的列组合在 Answer 和 QID 中。
也就是说,有如下Dataframe
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
我想得到以下结果:
QID Category Text QType Question: Answer0 Answer1 Answer2 Answer3 Country Answers
0 16 Automotive Access to car Single Do you have access to a car? 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 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 Prefer not to say']
到目前为止,我已经尝试了以下方法:
previous_qid = None
i = 0
j = 0
answers = []
new_row = {}
new_df = pd.DataFrame(columns=df.columns)
for _, row in df.iterrows():
# get QID
qid = row['QID']
if qid == previous_qid:
i+=1
new_row['Answer'+str(i)]=row['Answer0']
answers.append(row['Answer0'])
elif new_row != {}:
# we moved to a new row
new_row['QID'] = qid
new_row['Question'] = row['Question']
new_row['Answers'] = answers
# we create a new row in the new_dataframe
new_df.append(new_row, ignore_index=True)
# we clean up everything to receive the next row
answers = []
i=0
j+=1
new_row = {}
# we add the information of the current row
new_row['Answer'+str(i)]=row['Answer0']
answers.append(row['Answer0'])
previous_qid = qid
但new_df 结果为空。
【问题讨论】:
-
发布更多基本示例和预期结果。上述预期结果对我来说毫无意义。
标签: python python-3.x pandas dataframe