【问题标题】:Reading Quip Spreadsheet with quip-api and pandas使用 quip-api 和 pandas 阅读 Quip 电子表格
【发布时间】:2021-04-11 13:38:56
【问题描述】:

我已经开始探索 Quip API。

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

  1. 添加了电子表格的标题
  2. 在电子表格中添加了以下数据:
id name
1 harry
2 hermione
3 ron

这就是我尝试从 Quip 中阅读的方式:

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. 如何清理多余的行和列,只处理 pandas 数据帧中具有有效记录和标题的行作为idname
  3. 当我运行print(raw_df) 时添加raw_df.dropna(axis=0,inplace=True) 后,我得到None。为什么?

【问题讨论】:

    标签: python pandas quip


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-10-31
      • 2020-11-03
      • 2019-03-14
      • 2019-07-06
      • 1970-01-01
      • 2019-01-11
      • 2017-04-18
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多