熊猫,使用read_csv导入类似JSON的文件

马克·阿兰·弗兰克

我想将数据从.txt导入到dataframe。我无法使用经典pd.read_csv导入它,而使用不同类型的sep会引发错误。我要导入的数据Cell_Phones _&_ Accessories.txt.gz是一种格式。

product/productId: B000JVER7W
product/title: Mobile Action MA730 Handset Manager - Bluetooth Data Suite
product/price: unknown
review/userId: A1RXYH9ROBAKEZ
review/profileName: A. Igoe
review/helpfulness: 0/0
review/score: 1.0
review/time: 1233360000
review/summary: Don't buy!
review/text: First of all, the company took my money and sent me an email telling me the product was shipped. A week and a half later I received another email telling me that they are sorry, but they don't actually have any of these items, and if I received an email telling me it has shipped, it was a mistake.When I finally got my money back, I went through another company to buy the product and it won't work with my phone, even though it depicts that it will. I have sent numerous emails to the company - I can't actually find a phone number on their website - and I still have not gotten any kind of response. What kind of customer service is that? No one will help me with this problem. My advice - don't waste your money!

product/productId: B000JVER7W
product/title: Mobile Action MA730 Handset Manager - Bluetooth Data Suite
product/price: unknown
....
耶斯列尔

您可以使用jen分隔符,然后先按:进行分隔pivot

df = pd.read_csv('Cell_Phones_&_Accessories.txt', sep='¥', names=['data'], engine='python')

df1 = df.pop('data').str.split(':', n=1, expand=True)
df1.columns = ['a','b']

df1 = df1.assign(c=(df1['a'] == 'product/productId').cumsum())
df1 = df1.pivot('c','a','b')

带有defaultdictDataFrame构造函数的Python解决方案,用于提高性能:

from collections import defaultdict

data = defaultdict(list)
with open("Cell_Phones_&_Accessories.txt") as f:
  for line in f.readlines():
      if len(line) > 1:
          key, value = line.strip().split(':', 1)
          data[key].append(value)

df = pd.DataFrame(data)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章