第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數(shù)據(jù)框:單元格級別:將逗號分隔的字符串轉換為列表

數(shù)據(jù)框:單元格級別:將逗號分隔的字符串轉換為列表

慕運維8079593 2023-08-22 17:00:36
我有一個 CSV 文件,其中包含有關汽車行程的信息。我想整理這些數(shù)據(jù),以便為每個旅程(每一行)提供一個列表。該列表應包含作為列表中第一項的旅程代碼,然后將所有后續(xù) MGRS 單元作為單獨的項目。最后,我希望將所有這些旅程列表分組到父列表中。如果我手動執(zhí)行此操作,它將如下所示:journeyCodeA = ['journeyCodeA', 'mgrs1', 'mgrs2', 'mgrs3']journeyCodeB = ['journeyCodeB', 'mgrs2', 'mgrs4', 'mgrs7']combinedList = [journeyCodeA, journeyCodeB]這是迄今為止我為每行創(chuàng)建一個列表并組合所需列的內容。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']]我正在努力尋找一種方法來迭代數(shù)據(jù)幀的每一行,引用 mgrs_grids 列,然后將逗號分隔的字符串就地轉換為列表。
查看完整描述

2 回答

?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

用于pandas.Series.str.split將字符串拆分為list.

# 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

每個值單獨一行

  • 創(chuàng)建一列列表后。

  • 用于pandas.DataFrame.explode為列表中的每個值創(chuàng)建單獨的行。

# 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']]



查看完整回答
反對 回復 2023-08-22
?
RISEBY

TA貢獻1856條經(jīng)驗 獲得超5個贊

您還可以將數(shù)據(jù)框拆分并分解為表格格式。


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_grids

0      7211863  7211863-140  18TWL927129

0      7211863  7211863-140  18TWL888113

0      7211863  7211863-140  18TWL888113

0      7211863  7211863-140  18TWL887113

0      7211863  7211863-140  18TWL888113

0      7211863  7211863-140  18TWL887113

0      7211863  7211863-140  18TWL887113

0      7211863  7211863-140  18TWL887113

0      7211863  7211863-140  18TWL903128

1      7211863  7211863-105  18TWL927129

1      7211863  7211863-105  18TWL939112

1      7211863  7211863-105  18TWL939112

1      7211863  7211863-105  18TWL939113

1      7211863  7211863-105  18TWL939113

1      7211863  7211863-105  18TWL939113

1      7211863  7211863-105  18TWL939113

1      7211863  7211863-105  18TWL939113

1      7211863  7211863-105  18TWL939113

1      7211863  7211863-105  18TWL960111

1      7211863  7211863-105  18TWL960112

2      7211863   7211863-50  18TWL927129

2      7211863   7211863-50  18TWL889085

2      7211863   7211863-50  18TWL889085

2      7211863   7211863-50  18TWL888085

2      7211863   7211863-50  18TWL888085

2      7211863   7211863-50  18TWL888085

2      7211863   7211863-50  18TWL888085

2      7211863   7211863-50  18TWL888085

2      7211863   7211863-50  18TWL890085

3      7211863  7211863-109  18TWL927129

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952106

3      7211863  7211863-109  18TWL952105

3      7211863  7211863-109  18TWL951103


查看完整回答
反對 回復 2023-08-22
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號