从 DataFrame/csv 中删除空格

我想将 2 个 DataFrame 包含到 1 个 csv 中。

我的代码运行并打印 2 个 DataFrame,但是我想删除 D 列、EF 第 2 行中的空格,以便第 3 行中的信息出现在其标题的正下方。

数据是正确的,只是格式化是我的问题。

(如果您运行代码,这将有意义,文件非常小)

import pandas as pd

from bs4 import BeautifulSoup

import csv

import requests

line1=[]


url='https://clinicaltrials.gov/ct2/show/NCT03548207'

r=requests.get(url)

soup=BeautifulSoup(r.content,'html.parser')

content=soup.find_all('div',id='main-content')

for item in content:

    title=item.find('h1',class_='tr-h1 ct-sans-serif tr-solo_record').text

    sponsor=item.find('div', class_='tr-info-text').text

    summary=item.find('div',class_='ct-body3 tr-indent2').text

    dict={'Title':title,'Sponsor':sponsor,'Summary':summary}

    line1.append(dict)


df=pd.DataFrame(line1)



url2='https://clinicaltrials.gov/ct2/show/NCT03548207'

table1=pd.read_html(url2)[2]

dj=pd.DataFrame(table1)

kk=df.append(dj)

kk.to_csv('çode11.csv',index=False)


吃鸡游戏
浏览 100回答 2
2回答

繁华开满天机

您可以pd.concat与axis=1参数一起使用:import pandas as pdfrom bs4 import BeautifulSoupimport csvimport requestsline1=[]url='https://clinicaltrials.gov/ct2/show/NCT03548207'r=requests.get(url)soup=BeautifulSoup(r.content,'html.parser')content=soup.find_all('div',id='main-content')for item in content:&nbsp; &nbsp; title=item.find('h1',class_='tr-h1 ct-sans-serif tr-solo_record').text&nbsp; &nbsp; sponsor=item.find('div', class_='tr-info-text').text&nbsp; &nbsp; summary=item.find('div',class_='ct-body3 tr-indent2').text&nbsp; &nbsp; dict={'Title':title,'Sponsor':sponsor,'Summary':summary}&nbsp; &nbsp; line1.append(dict)df=pd.DataFrame(line1)url2='https://clinicaltrials.gov/ct2/show/NCT03548207'table1=pd.read_html(url2)[2]dj=pd.DataFrame(table1)kk = pd.concat([df, dj], axis=1)&nbsp; # <--- pd.concatprint(kk)kk.to_csv('data.csv', index=False)印刷:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Title&nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Phase0&nbsp; A Study of JNJ-68284528, a Chimeric Antigen Re...&nbsp; ...&nbsp; Phase 1&nbsp; Phase 2[1 rows x 6 columns]并保存data.csv(来自 LibreOffice 的屏幕截图):

慕森卡

转变能解决你的问题吗?import pandas as pddf = pd.DataFrame({"a":[0,1,2,3,4,5], 'b':[10, None, 12, 13, 14, 15], 'c':[20, None, 22, 23, 24, 25]})df.loc[1:,'b':'c']=df.loc[1:,'b':'c'].shift(-1)print(df)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python