gpt4 book ai didi

python - 数据框 : Cell Level: Convert Comma Separated String to List

转载 作者:行者123 更新时间:2023-12-05 08:19:56 26 4
gpt4 key购买 nike

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

enter image description here

我想处理这些数据,以便每次旅程(每一行)都有一个列表。该列表应包含 journey_code 作为列表中的第一项,然后所有后续 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 列,然后将逗号分隔的字符串就地转换为列表。

感谢您的帮助!


{'driver_code': {0: 7211863, 1: 7211863, 2: 7211863, 3: 7211863},
'journey_code': {0: '7211863-140',
1: '7211863-105',
2: '7211863-50',
3: '7211863-109'},
'mgrs_grids': {0: '18TWL927129,18TWL888113,18TWL888113,18TWL887113,18TWL888113,18TWL887113,18TWL887113,18TWL887113,18TWL903128',
1: '18TWL927129,18TWL939112,18TWL939112,18TWL939113,18TWL939113,18TWL939113,18TWL939113,18TWL939113,18TWL939113,18TWL960111,18TWL960112',
2: '18TWL927129,18TWL889085,18TWL889085,18TWL888085,18TWL888085,18TWL888085,18TWL888085,18TWL888085,18TWL890085',
3: '18TWL927129,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952106,18TWL952105,18TWL951103'}}

最佳答案

# use str split on the column
df.mgrs_grids = df.mgrs_grids.str.split(',')

# display(df)
driver_code journey_code mgrs_grids
0 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

每个值单独一行

# get a separate row for each value
df = df.explode('mgrs_grids').reset_index(drop=True)

# display(df.hea())
driver_code journey_code mgrs_grids
0 7211863 7211863-140 18TWL927129
1 7211863 7211863-140 18TWL888113
2 7211863 7211863-140 18TWL888113
3 7211863 7211863-140 18TWL887113
4 7211863 7211863-140 18TWL888113

更新

  • 这里是另一种选择,将'journey_code'组合到'mgrs_grids'的前面,然后将字符串拆分成一个列表。
    • 此列表分配回 'mgrs_grids',但也可以分配给新列。
# add the journey code to mgrs_grids and then split
df.mgrs_grids = (df.journey_code + ',' + df.mgrs_grids).str.split(',')

# display(df.head())
driver_code journey_code mgrs_grids
0 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 list
df.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']]

关于python - 数据框 : Cell Level: Convert Comma Separated String to List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63982912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com