我正在尝试将这两列按元素组合成一个新列,例如,第一行应该是:
['332', '331'] + ['C/A/2/3', 'C/A/2/3'] = ['332C/A/2/3', '331C/A/2/3']这是一个很大的数据集,所以一个更快的方法可以节省很多时间。
数据集中的列是

发布于 2020-07-19 05:47:40
mapping = {'stops': [['332', '331'],
['327', '331', '332'],
['015', '014', '013', '012', '011'],
['011', '013', '014', '015'],
['148', '161']],
'routes': [['C/A/2/3', 'C/A/2/3'],
['2/3', '2/3', '2/3'],
['N/R', 'N/R', 'N/R', 'N/R', 'N/R'],
['N/R', 'N/R', 'N/R', 'N/R'],
['C/A/1', 'C/A/1']]}
df = pd.DataFrame(mapping)
df['merged'] = [["".join(entry) for entry in zip(*ent)]
for ent in zip(df.stops, df.routes)]发布于 2020-07-19 04:11:05
这也可以这样做
stops_routes=[]
for i in range(df.shape[0]): #loop over all the rows
lst=[]
for j in range(len(df['stops'][i])): #as their are variable number of elements in every rows
lst.append(df['stops'][i][j]+df['routes'][i][j]) #add the elements(that are string) inside the list
stops_routes.append(lst) #append it to out final list this happens for every row
df['stops_routes']=stops_routes #creating a new column as stops_routes in the data frame and giving the values to the column 我试着用注释解释每一行代码。
https://stackoverflow.com/questions/62976017
复制相似问题