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

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

如何根據(jù)條件將數(shù)據(jù)框列拆分為單獨的列

如何根據(jù)條件將數(shù)據(jù)框列拆分為單獨的列

有只小跳蛙 2023-03-16 09:38:09
我正在嘗試將以下數(shù)據(jù)框拆分為單獨的列。我希望一列中的所有文本和數(shù)字在空白處分開。df[0].head(10)0                                                   []1               [Andaman and Nicobar, 194, 52, 142, 0]2        [Andhra Pradesh, 40,646, 19,814, 20,298, 534]3                [Arunachal Pradesh, 609, 431, 175, 3]4                   [Assam, 20,646, 6,490, 14,105, 51]5                  [Bihar, 23,589, 8,767, 14,621, 201]6                      [Chandigarh, 660, 169, 480, 11]7              [Chhattisgarh, 4,964, 1,429, 3,512, 23]8    [Dadra and Nagar Haveli and Daman, 585, 182, 4...9                          [Daman and Diu, 0, 0, 0, 0]Name: 0, dtype: object如果我只在空白處拆分并展開,雖然數(shù)字被正確拆分,但文本被拆分成多列。由于不同觀察的文本跨越不同數(shù)量的列,我無法再次連接它們。顯然,解決方案是編寫正確的“正則表達(dá)式”并對其進(jìn)行拆分。我無法弄清楚所需的正則表達(dá)式,因此請求輸入。df1 = df[0].str.split(' ', expand= True)df1.head(10)    0   1   2   3   4   5   6   7   8   90   []  None    None    None    None    None    None    None    None    None1   [Andaman    and     Nicobar,    194,    52,     142,    0]  None    None    None2   [Andhra     Pradesh,    40,646,     19,814,     20,298,     534]    None    None    None    None3   [Arunachal  Pradesh,    609,    431,    175,    3]  None    None    None    None4   [Assam,     20,646,     6,490,  14,105,     51]     None    None    None    None    None5   [Bihar,     23,589,     8,767,  14,621,     201]    None    None    None    None    None6   [Chandigarh,    660,    169,    480,    11]     None    None    None    None    None7   [Chhattisgarh,  4,964,  1,429,  3,512,  23]     None    None    None    None    None8   [Dadra  and     Nagar   Haveli  and     Daman,  585,    182,    401,    2]9   [Daman  and     Diu,    0,  0,  0,  0]  None    None    None
查看完整描述

1 回答

?
揚帆大魚

TA貢獻(xiàn)1799條經(jīng)驗 獲得超9個贊

您可以使用str.replace并str.extract重新塑造您的數(shù)據(jù)框。


names = df[0].str.extract('(\D+)').replace('\[|,','',regex=True).rename(columns={0 : 'names'})



df_new = names.join(df[0].str.replace('\D+,','').str.strip(']').str.split(' ',expand=True))

print(df_new)


                                  names 0        1        2        3     4

0                   Andaman and Nicobar       194,      52,     142,     0

1                        Andhra Pradesh    40,646,  19,814,  20,298,   534

2                     Arunachal Pradesh       609,     431,     175,     3

3                                 Assam    20,646,   6,490,  14,105,    51

4                                 Bihar    23,589,   8,767,  14,621,   201

5                            Chandigarh       660,     169,     480,    11

6                          Chhattisgarh     4,964,   1,429,   3,512,    23

7      Dadra and Nagar Haveli and Daman       585,     182,     4...  None

8                         Daman and Diu         0,       0,       0,     0


查看完整回答
反對 回復(fù) 2023-03-16
  • 1 回答
  • 0 關(guān)注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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