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

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

在Python中從目錄中的多個CSV文件中提取特定列

在Python中從目錄中的多個CSV文件中提取特定列

牛魔王的故事 2023-10-11 16:15:17
我的目錄中有大約 200 個 CSV 文件,其中包含不同的列,但有些文件包含我想要提取的數(shù)據(jù)。我想要拉的一列稱為“程序”(行的順序不同,但名稱相同),另一列包含“建議”(并非所有措辭都相同,但它們都會包含該措辭)。最終,我想為每個 CSV 提取這些列下的所有行,并將它們附加到僅包含這兩列的數(shù)據(jù)框中。我曾嘗試先使用一個 CSV 執(zhí)行此操作,但無法使其工作。這是我嘗試過的:import pandas as pdfrom io import StringIOdf =  pd.read_csv("test.csv")dfout = pd.DataFrame(columns=['Programme', 'Recommends'])for file in [df]:    dfn = pd.read_csv(file)    matching = [s for s in dfn.columns if "would recommend" in s]    if matching:        dfn = dfn.rename(columns={matching[0]:'Recommends'})        dfout = pd.concat([dfout, dfn], join="inner")print(dfout)我收到以下錯誤消息,所以我認(rèn)為這是格式問題(它不喜歡 pandas df?): ValueError(msg.format(_type=type(filepath_or_buffer))) ValueError: 無效的文件路徑或緩沖區(qū)對象類型: <類'pandas.core.frame.DataFrame'>當(dāng)我嘗試這個時:csv1 = StringIO("""Programme,"Overall, I am satisfied with the quality of the programme",I would recommend the company to a friend or colleague,Please comment on any positive aspects of your experience of this programmeNursing,4,4,IMAGENursing,1,3,very goodNursing,4,5,I enjoyed studying tis programme""")csv2 = StringIO("""Programme,I would recommend the company to a friend,The programme was well organised and running smoothly,It is clear how students' feedback on the programme has been acted onIT,4,2,4IT,5,5,5IT,5,4,5""")dfout = pd.DataFrame(columns=['Programme', 'Recommends'])for file in [csv1,csv2]:    dfn = pd.read_csv(file)    matching = [s for s in dfn.columns if "would recommend" in s]    if matching:        dfn = dfn.rename(columns={matching[0]:'Recommends'})        dfout = pd.concat([dfout, dfn], join="inner")print(dfout)這工作正常,但我需要讀取 CSV 文件。有任何想法嗎?上面示例的預(yù)期輸出: 
查看完整描述

1 回答

?
撒科打諢

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

以下作品:


import pandas as pd

import glob


dfOut = []


for myfile in glob.glob("*.csv"):

    tmp = pd.read_csv(myfile, encoding='latin-1')

    

    matching = [s for s in tmp.columns if "would recommend" in s]

    if len(matching) > 0:

        tmp.rename(columns={matching[0]: 'Recommend'}, inplace=True)

        tmp = tmp[['Subunit', 'Recommend']]

        dfOut.append(tmp)

        

df = pd.concat(dfOut)


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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