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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用str(df)后如何取回DataFrame?

使用str(df)后如何取回DataFrame?

紅糖糍粑 2021-10-19 09:45:33
我想我在嘗試保存包含一堆 Pandas 數(shù)據(jù)幀的 Pandas 系列時(shí)搞砸了。事實(shí)證明,每個(gè) DataFrame 都像我調(diào)用df.to_string()它們一樣被保存。從我目前的觀察來(lái)看,我的字符串在某些地方有額外的間距,\當(dāng) DataFrame 有太多列無(wú)法在同一行上顯示時(shí),我的字符串也會(huì)有額外的間距。這是一個(gè)“更合適的數(shù)據(jù)幀:df = pd.DataFrame(columns=["really long name that goes on for a while", "another really long string", "c"]*6,                   data=[["some really long data",2,3]*6,[4,5,6]*6,[7,8,9]*6])我擁有并希望變成 DataFrame 的字符串如下所示:# str(df)'  really long name that goes on for a while  another really long string  c  \\\n0                     some really long data                           2  3   \n1                                         4                           5  6   \n2                                         7                           8  9   \n\n  really long name that goes on for a while  another really long string  c  \\\n0                     some really long data                           2  3   \n1                                         4                           5  6   \n2                                         7                           8  9   \n\n  really long name that goes on for a while  another really long string  c  \\\n0                     some really long data                           2  3   \n1                                         4                           5  6   \n2                                         7                           8  9   \n\n  really long name that goes on for a while  another really long string  c  \\\n0                     some really long data                           2  3   \n1                                         4                           5  6   \n2                                         7                           8  9   \n\n  really long name that goes on for a while  another really long string  c  \\\n0                     some really long data                           2  3   \n1      我如何將這樣的字符串還原回 DataFrame?謝謝
查看完整描述

3 回答

?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

嘗試這個(gè)。更新為包含自動(dòng)計(jì)算行數(shù)的邏輯?;旧衔姨崛≡紨?shù)據(jù)幀索引(行號(hào))的最大值,它在大字符串內(nèi)。


如果我們從使用您提供的示例轉(zhuǎn)換為字符串的數(shù)據(jù)幀開始:


df = pd.DataFrame(columns=["really long name that goes on for a while", "another really long string", "c"]*6, 

                  data=[["some really long data",2,3]*6,[4,5,6]*6,[7,8,9]*6])


string = str(df)

首先,讓我們提取列名:

import re

import numpy as np


lst = re.split('\n', string)

num_rows = int(lst[lst.index('') -1][0]) + 1

col_names = []

lst = [i for i in lst if i != '']


for i in range(0,len(lst), num_rows + 1):

    col_names.append(lst[i])


new_col_names = []

for i in col_names:

    new_col_names.append(re.split('  ', i))


final_col_names = []

for i in new_col_names:

    final_col_names += i


final_col_names = [i for i in final_col_names if i != '']

final_col_names = [i for i in final_col_names if i != '\\']

然后,讓我們獲取數(shù)據(jù):

for i in col_names:

    lst.remove(i)


new_lst = [re.split(r'\s{2,}', i) for i in lst]

new_lst = [i[1:-1] for i in new_lst]


newer_lst = []

for i in range(num_rows):

    sub_lst = []

    for j in range(i,len(final_col_names), num_rows):

        sub_lst += new_lst[j]

    newer_lst.append(sub_lst)


reshaped = np.reshape(newer_lst, (num_rows,len(final_col_names)))

最后,我們可以使用數(shù)據(jù)和列名創(chuàng)建重建的數(shù)據(jù)框:

fixed_df = pd.DataFrame(data=reshaped, columns = final_col_names)

我的代碼執(zhí)行了一些循環(huán),因此如果您的原始數(shù)據(jù)幀有數(shù)十萬(wàn)行,這種方法可能需要一段時(shí)間。


查看完整回答
反對(duì) 回復(fù) 2021-10-19
  • 3 回答
  • 0 關(guān)注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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