数据框:单元格级别:将逗号分隔的字符串转换为列表

我有一个 CSV 文件,其中包含有关汽车行程的信息。

https://img4.mukewang.com/64e479770001dd6606510188.jpg

我想整理这些数据,以便为每个旅程(每一行)提供一个列表。该列表应包含作为列表中第一项的旅程代码,然后将所有后续 MGRS 单元作为单独的项目。最后,我希望将所有这些旅程列表分组到父列表中。


如果我手动执行此操作,它将如下所示:


journeyCodeA = ['journeyCodeA', 'mgrs1', 'mgrs2', 'mgrs3']

journeyCodeB = ['journeyCodeB', 'mgrs2', 'mgrs4', 'mgrs7']

combinedList = [journeyCodeA, journeyCodeB]

这是迄今为止我为每行创建一个列表并组合所需列的内容。


comparison_journey_mgrs = pd.read_csv(r"journey-mgrs.csv", delimiter = ',')

comparison_journey_mgrs['mgrs_grids'] = comparison_journey_mgrs['mgrs_grids'].str.replace(" ","")

comparison_journey_list = []


for index, rows in comparison_route_mgrs.iterrows():

        holding_list = [rows.journey_code, rows.mgrs_grids]

        comparison_journey_list.append(holding_list)

这样做的问题是它将 mgrs_grids 列视为单个字符串。


我的清单如下所示:


[['7211863-140','18TWL927129,18TWL888113,18TWL888113,...,18TWL903128']]

但我希望它看起来像这样:


[['7211863-140','18TWL927129', '18TWL888113', '18TWL888113',..., '18TWL903128']]

我正在努力寻找一种方法来迭代数据帧的每一行,引用 mgrs_grids 列,然后将逗号分隔的字符串就地转换为列表。


慕运维8079593
浏览 97回答 2
2回答

慕姐4208626

用于pandas.Series.str.split将字符串拆分为list.# use str split on the columndf.mgrs_grids = df.mgrs_grids.str.split(',')# display(df)   driver_code journey_code                                                                                                                                       mgrs_grids0      7211863  7211863-140                            [18TWL927129, 18TWL888113, 18TWL888113, 18TWL887113, 18TWL888113, 18TWL887113, 18TWL887113, 18TWL887113, 18TWL903128]1      7211863  7211863-105  [18TWL927129, 18TWL939112, 18TWL939112, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL960111, 18TWL960112]2      7211863   7211863-50                            [18TWL927129, 18TWL889085, 18TWL889085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL890085]3      7211863  7211863-109               [18TWL927129, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952105, 18TWL951103]print(type(df.loc[0, 'mgrs_grids']))[out]:list每个值单独一行创建一列列表后。用于pandas.DataFrame.explode为列表中的每个值创建单独的行。# get a separate row for each valuedf = df.explode('mgrs_grids').reset_index(drop=True)# display(df.hea())   driver_code journey_code   mgrs_grids0      7211863  7211863-140  18TWL9271291      7211863  7211863-140  18TWL8881132      7211863  7211863-140  18TWL8881133      7211863  7211863-140  18TWL8871134      7211863  7211863-140  18TWL888113更新这是另一个选项,它将 组合'journey_code'到 的前面'mgrs_grids',然后将字符串拆分为列表。该列表被分配回'mgrs_grids',但也可以分配给新列。# add the journey code to mgrs_grids and then splitdf.mgrs_grids = (df.journey_code + ',' + df.mgrs_grids).str.split(',')# display(df.head())   driver_code journey_code                                                                                                                                                    mgrs_grids0      7211863  7211863-140                            [7211863-140, 18TWL927129, 18TWL888113, 18TWL888113, 18TWL887113, 18TWL888113, 18TWL887113, 18TWL887113, 18TWL887113, 18TWL903128]1      7211863  7211863-105  [7211863-105, 18TWL927129, 18TWL939112, 18TWL939112, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL939113, 18TWL960111, 18TWL960112]2      7211863   7211863-50                             [7211863-50, 18TWL927129, 18TWL889085, 18TWL889085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL888085, 18TWL890085]3      7211863  7211863-109               [7211863-109, 18TWL927129, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952106, 18TWL952105, 18TWL951103]# output to nested listdf.mgrs_grids.tolist()[out]:[['7211863-140', '18TWL927129', '18TWL888113', '18TWL888113', '18TWL887113', '18TWL888113', '18TWL887113', '18TWL887113', '18TWL887113', '18TWL903128'], ['7211863-105', '18TWL927129', '18TWL939112', '18TWL939112', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL939113', '18TWL960111', '18TWL960112'], ['7211863-50', '18TWL927129', '18TWL889085', '18TWL889085', '18TWL888085', '18TWL888085', '18TWL888085', '18TWL888085', '18TWL888085', '18TWL890085'], ['7211863-109', '18TWL927129', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952106', '18TWL952105', '18TWL951103']]

RISEBY

您还可以将数据框拆分并分解为表格格式。df1 = df.join(df['mgrs_grids'].str.split(',',expand=True).stack().reset_index(1),how='outer')\        .drop(['level_1','mgrs_grids'],1).rename(columns={0 : 'mgrs_grids'})print(df1)   driver_code journey_code   mgrs_grids0      7211863  7211863-140  18TWL9271290      7211863  7211863-140  18TWL8881130      7211863  7211863-140  18TWL8881130      7211863  7211863-140  18TWL8871130      7211863  7211863-140  18TWL8881130      7211863  7211863-140  18TWL8871130      7211863  7211863-140  18TWL8871130      7211863  7211863-140  18TWL8871130      7211863  7211863-140  18TWL9031281      7211863  7211863-105  18TWL9271291      7211863  7211863-105  18TWL9391121      7211863  7211863-105  18TWL9391121      7211863  7211863-105  18TWL9391131      7211863  7211863-105  18TWL9391131      7211863  7211863-105  18TWL9391131      7211863  7211863-105  18TWL9391131      7211863  7211863-105  18TWL9391131      7211863  7211863-105  18TWL9391131      7211863  7211863-105  18TWL9601111      7211863  7211863-105  18TWL9601122      7211863   7211863-50  18TWL9271292      7211863   7211863-50  18TWL8890852      7211863   7211863-50  18TWL8890852      7211863   7211863-50  18TWL8880852      7211863   7211863-50  18TWL8880852      7211863   7211863-50  18TWL8880852      7211863   7211863-50  18TWL8880852      7211863   7211863-50  18TWL8880852      7211863   7211863-50  18TWL8900853      7211863  7211863-109  18TWL9271293      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521063      7211863  7211863-109  18TWL9521053      7211863  7211863-109  18TWL951103
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python