【问题标题】:Convert a list into an arranged dataframe in pandas将列表转换为熊猫中的排列数据框
【发布时间】:2021-03-22 06:21:12
【问题描述】:

我的列表存储在info 中,其中包含 -

['Stephanie Ramirez', 'Greater Tampa Bay Area', '500+ connections', 'Jacobs', 'Technical Recruiter', '2019 – Present', '2 yrs']

我希望它如何出现在数据框中,以便以后可以将其导出到 excel 文件中,例如 -

name location connection company position duration tenure
Stephanie Ramirez Greater Tampa Bay Area 500+ connections Jacobs Technical Recruiter 2019 – Present 2 yrs

这是我的代码,它提取数据并附加到info

name_div = soup.find('div', {'class': 'flex-1 mr5'})
name_loc = name_div.find_all('ul')
name = name_loc[0].find('li').get_text().strip()
loc = name_loc[1].find('li').get_text().strip()
connection = name_loc[1].find_all('li')
connection = connection[1].get_text().strip()
info = []
info.append(name)
info.append(loc)
info.append(connection)
info

['Stephanie Ramirez', 'Greater Tampa Bay Area', '500+ connections']

#experience setion
exp_section = soup.find('section', {'id': 'experience-section'})
exp_section = exp_section.find('ul')
div_tag = exp_section.find('div')
a_tag = div_tag.find('a')
job_title = a_tag.find('h3').get_text().strip()
company_name = a_tag.find_all('p')[1].get_text().strip()
joining_date = a_tag.find_all('h4')[0].find_all('span')[1].get_text().strip()
exp = a_tag.find_all('h4')[1].find_all('span')[1].get_text().strip()
info.append(company_name)
info.append(job_title)
info.append(joining_date)
info.append(exp)
info

['Stephanie Ramirez', 'Greater Tampa Bay Area', '500+ connections', 'Jacobs', 'Technical Recruiter', '2019 – Present', '2 yrs']

请帮助我在数据框中获得所需的 OP。 提前致谢!!

只是问一下,我发布问题的方式是否正确?

【问题讨论】:

    标签: pandas dataframe jupyter-notebook


    【解决方案1】:

    使用df.T:

    In [131]: import pandas as pd
    
    In [132]: info = ['Stephanie Ramirez', 'Greater Tampa Bay Area', '500+ connections', 'Jacobs', 'Technical Recruiter', '2019 – Present', '2 yrs']
    
    In [137]: df = pd.DataFrame(info).T
    
    In [139]: df.columns = ['name', 'location', 'connection', 'company', 'position', 'duration', 'tenure']
    
    In [140]: df
    Out[140]: 
                    name                location        connection company             position        duration tenure
    0  Stephanie Ramirez  Greater Tampa Bay Area  500+ connections  Jacobs  Technical Recruiter  2019 – Present  2 yrs
    

    【讨论】:

    • 先生,非常感谢它的工作,我得到了我想要的 OP。先生,我还有一个与此帖子相关的问题,需要解决可以在评论中提问吗?..您允许吗?
    • @Yes_I_needHelp 最好发布一个新问题。
    • 我只需要问一下,因为我正在从配置文件中提取数据,因此代码会在未填写详细信息的情况下引发错误,例如 - 在这种情况下未提及位置会给出错误,楼主怎么解决的?
    • 我需要更多信息。
    • 先生,如果您允许,我可以分享 WETRANSFER 链接吗?..我将发布我的代码和错误所在配置文件的链接?
    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 2016-09-25
    • 2017-08-26
    • 1970-01-01
    • 2018-08-25
    • 2022-01-19
    • 2023-03-30
    相关资源
    最近更新 更多