简体   繁体   English

Python DataFrame 将类型为 pandas.core.series.Series 的两列合并为单列

[英]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.

相关问题 如何划分两个类型为`pandas.core.series.Series`的列? - How to divide two columns of type `pandas.core.series.Series`? 如何将 pandas.core.series.Series 分配给 pandas dataframe 列 - how to assign a pandas.core.series.Series to a pandas dataframe column 将 pandas.core.series.Series 转换为具有多个列名的数据框 - Converting pandas.core.series.Series to dataframe with multiple column names 如何合并 pandas.core.series.Series 类型的两个向量? - How to merge two vectors of type pandas.core.series.Series? pandas.core.series.series 到 dataframe - pandas.core.series.series to dataframe 使用适当的列值python将pandas.core.series.Series转换为dataframe - Converting pandas.core.series.Series to dataframe with appropriate column values python 错误:object 类型没有轴名为 1<class 'pandas.core.series.series'> 在 python forloop 在 DataFrame</class> - Error: No axis named 1 for object type <class 'pandas.core.series.Series'> in python forloop on DataFrame 如何将具有多个索引的 pandas.core.series.Series object 转换为 pandas ZC699575A5E8AFD9EZFBCA1A 填充的所有列? - How to convert pandas.core.series.Series object with Multiple index to a pandas Dataframe with all columns filled? 从pandas数据框列中的pandas.core.series.Series中提取日期 - Extract date from pandas.core.series.Series in pandas dataframe columns Python Pandas:向类pandas.core.series.Series添加方法 - Python Pandas: Adding methods to class pandas.core.series.Series
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM