python怎么用pandas读取数据_如何在python/pandas中以段落形式读取数据?

您没有确切地说明数据是如何存储的,所以我假设您有一堆与上面的类似的项,这些项是通过在文件中读取并在新行上拆分得到的。这将提供一个类似示例数据的字符串。在

将该字符串传递给函数:def read_year(f):

f = iter(f.split('\n')) # split the string, going to iterate over it

year = next(f).strip()

title = next(f).strip() # maybe split at the '-', dunno if that's part of the title

# don't need opening date, so iterate till we get there.

people = takewhile(lambda x: not x.strip().startswith('Opening'), f)

# setting up for getting (kind, individual)

people = (tuple(x.split(':')) for x in people)

both = []

for kind, persons in people:

kind_ = kind.strip()

for person in persons.strip().split(' '):

if person != 'and':

both.append((kind_, person.strip().strip(',')))

df = pd.DataFrame(both, columns=['person_title', 'person'])

df['year'] = int(year)

df['movie_title'] = title

return df

基本上,在每个字符串上调用该函数

^{pr2}$

然后pd.concat与{}一起使用。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值