[英]Kobo Comma Separated Loop Data to List - Python
We have a kobo survey that collects data in loops.我们有一个循环收集数据的 kobo 调查。 We ask each person how many devices you own and when was the purchase date for each.我们询问每个人您拥有多少台设备,以及每台设备的购买日期。 The output data looks like this: output 数据如下所示:
I want to import the excel data into a python data frame then do the clean up to have it like this:我想将 excel 数据导入 python 数据框,然后进行清理以使其如下所示:
I have been able to import the data but got stuck at the implementation and cleanup:我已经能够导入数据,但陷入了实施和清理:
import pandas as pd
df = pd.read_excel('DataFile.xlsx')
print(df)
.
.
.
.
Firstly make use of rstrip()
method and split()
method:首先使用rstrip()
方法和split()
方法:
df['Devices']=df['Devices'].str.rstrip(';').str.split(';')
df['Purchase Date']=df['Purchase Date'].str.rstrip(';').str.split(';')
Finally use explode()
method:最后使用explode()
方法:
df_out=df.explode('Devices')
df_out['Purchase Date']=df['Purchase Date'].explode()
Now If you print df_out
you will get your desired output现在如果你打印df_out
你会得到你想要的 output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.