python - 使用 pd.read_clipboard 读取打印精美/格式化的数据框?

标签 python pandas dataframe clipboard

这是来自另一个 question 的数据框:

+--------------------+---------------+-------+
|   Location         | Date          | Value |
+--------------------+---------------+-------+
| India              | 2015-03-15    |   -200|
| India              | 2015-02-15    |  140  |
| India              | 2015-01-15    |  155  |
| India              | 2015-12-15    |   85  |
| India              | 2015-11-15    |   45  |
| China              | 2015-03-15    |   199 |
| China              | 2015-02-15    |  164  |
| China              | 2015-01-15    |  209  |
| China              | 2015-12-15    |   24  |
| China              | 2015-11-15    |   11  |
| Russia             | 2015-03-15    |   48  |
| Russia             | 2015-02-15    |  104  |
| Russia             | 2015-01-15    |  106  |
| Russia             | 2015-12-15    |   -20 |
| Russia             | 2015-11-15    |   10  |
+--------------------+---------------+-------+

并且,为了方便起见,这里是您可以毫无问题地复制的版本:

   Location        Date  Value
0     India  2015-03-15   -200
1     India  2015-02-15    140
2     India  2015-01-15    155
3     India  2015-12-15     85
4     India  2015-11-15     45
5     China  2015-03-15    199
6     China  2015-02-15    164
7     China  2015-01-15    209
8     China  2015-12-15     24
9     China  2015-11-15     11
10   Russia  2015-03-15     48
11   Russia  2015-02-15    104
12   Russia  2015-01-15    106
13   Russia  2015-12-15    -20
14   Russia  2015-11-15     10

您将如何使用 df.read_clipboard 阅读它而无需手动删除所有这些分隔符和行分隔符?

如果不是 ---+----,使用 sepdelimiter 会很容易。

最佳答案

In [129]: pd.read_clipboard(comment='+', sep='\s*\|\s*', usecols=[1,2,3], engine='python')
Out[129]:
   Location        Date  Value
0     India  2015-03-15   -200
1     India  2015-02-15    140
2     India  2015-01-15    155
3     India  2015-12-15     85
4     India  2015-11-15     45
5     China  2015-03-15    199
6     China  2015-02-15    164
7     China  2015-01-15    209
8     China  2015-12-15     24
9     China  2015-11-15     11
10   Russia  2015-03-15     48
11   Russia  2015-02-15    104
12   Russia  2015-01-15    106
13   Russia  2015-12-15    -20
14   Russia  2015-11-15     10

关于python - 使用 pd.read_clipboard 读取打印精美/格式化的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856926/

相关文章:

python - 对特定行应用列减法

python - 有条件地创建(填充)一列,该列必须处理数据框中的行以匹配条件

Python fromtimestamp() 方法不一致

Python/Mongoengine - 保存到数据库时缺少时区?

python - 有效地找到由 numpy 数组的索引分割的子数组的 cumsum

python - 如何在 matplotlib 中向水平条形图添加标签?

python - 非法指令 : 4 when importing tensorflow in python 3. 6

python - pandas - 分割字符串并取出每对

python - 属性错误: 'function' object has no attribute 'bar' in pandas

scala - 收集要设置的 Spark 数据帧列值