[英]Python DataFrame Combine Two Columns with type pandas.core.series.Series into a Single Column
I'm trying to combine these two columns to a new column by elements in pandas, for example, the first row should become:我试图通过 pandas 中的元素将这两列组合成一个新列,例如,第一行应该变成:
['332', '331'] + ['C/A/2/3', 'C/A/2/3'] = ['332C/A/2/3', '331C/A/2/3']
This is a large dataset, so a faster method would save a lot of time.这是一个大型数据集,因此更快的方法可以节省大量时间。
the columns in the dataset are数据集中的列是
this can be done like this also这也可以这样做
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
I have tried to explain every line of code using comments.我试图用注释来解释每一行代码。
Zip the two columns, then join the zipped entries: Zip这两列,然后加入压缩条目:
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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.