用quipapi和pandas阅读Quip电子表格

2024-06-25 22:39:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经开始探索quipapi

我用Quip创建了一个电子表格,其中包含以下详细信息:

  1. 添加了电子表格的标题
  2. 在电子表格中添加以下数据:
^{tb1}$

下面是我如何从这句俏皮话中读到的:

import quip
import pandas as pd
import numpy as np
import html5lib

client = quip.QuipClient(token, base_url = baseurl)
rawdictionary = client.get_thread(thread_id)

dfs=pd.read_html(rawdictionary['html'])
raw_df = dfs[0]
raw_df.drop(raw_df.columns[[0]], axis = 1, inplace = True) 
#raw_df.dropna(axis=0,inplace=True)
print(raw_df.replace(r'^\s+$', np.nan, regex=True))

我尝试删除带有nan对象的行,并尝试用nan替换空白字符串。但是,我仍然看到这些空行和空列出现在数据帧中,例如:

         A         B  C  D  E  F  G  H  I  J  K  L  M  N  O  P
0   id      name  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
1    1    harry  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
2    2  hermione  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
3    3  ron  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
4    ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
5    ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
6    ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
7    ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
8    ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
9    ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
10   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
11   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
12   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
13   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
14   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
15   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
16   ​         ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​
17   ​     

​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​  ​

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

问题

  1. 通过Python阅读Quip电子表格的最佳方式是什么
  2. 如何清理额外的行和列,并仅处理数据帧中具有有效记录和头的行,如idname
  3. 在运行print(raw_df)时添加raw_df.dropna(axis=0,inplace=True)之后,我得到None。为什么?

Tags: 数据importidtruedfrawasnp
1条回答
网友
1楼 · 发布于 2024-06-25 22:39:03

Quip会自动拉入一些额外的空白列和行,其中包含\u200b unicode字符

我就是这样解决的:

import quip
import pandas as pd
import numpy as np
import html5lib

client = quip.QuipClient(token, base_url = baseurl)
rawdictionary = client.get_thread(thread_id)

dfs=pd.read_html(rawdictionary['html'])
raw_df = dfs[0]

raw_df.columns=raw_df.iloc[0] #Make first row as column header
raw_df=raw_df[1:] #After the above step, the 1st two rows become duplicate. Delete the 1st row.
raw_df=raw_df[attribs]
cleaned_df = raw_df.replace(np.nan, 'N/A')
cleaned_df = cleaned_df.replace('\u200b', np.nan) 
cleaned_df.dropna(axis=0,how='any',inplace=True)

相关问题 更多 >