python - 如何使用 to_clipboard() 提供 DataFrame 的可复制副本

标签 python python-3.x pandas dataframe jupyter-notebook

2018-09-18_reproducible_dataframe.ipynb

  • 此问题之前已标记为 How to make good reproducible pandas examples 的重复问题。
    • 如果您需要制作合成(虚假)数据来共享,请回答该问题。
    • 其他问题和相关答案涉及如何创建可重现的数据框。
    • 它们不涵盖如何复制现有数据框 .to_clipboard ,而这个问题具体涵盖了 .to_clipboard
<小时/>
  • 这似乎是一个显而易见的问题。然而,许多提出有关 Pandas 问题的用户都是新用户,缺乏经验。
  • 提出问题的一个关键组成部分是 How to create a Minimal, Complete, and Verifiable example ,它解释了“什么”和“为什么”,但没有解释“如何”。
<小时/>

例如,如OP ,我可能有以下数据框:

  • 在本示例中,我创建了合成数据,这是创建可重现数据集的一个选项,但不属于本问题的范围。
  • 想想这一点,就好像您已经加载了一个文件,并且只需要共享其中的一部分,即可重现错误。
import pandas as pd
import numpy as np
from datetime import datetime
from string import ascii_lowercase as al

np.random.seed(365)
rows = 15
cols = 2
data = np.random.randint(0, 10, size=(rows, cols))
index = pd.bdate_range(datetime.today(), freq='d', periods=rows)

df = pd.DataFrame(data=data, index=index, columns=list(al[:cols]))

            a  b
2020-07-30  2  4
2020-07-31  1  5
2020-08-01  2  2
2020-08-02  9  8
2020-08-03  4  0
2020-08-04  3  3
2020-08-05  7  7
2020-08-06  7  0
2020-08-07  8  4
2020-08-08  3  2
2020-08-09  6  2
2020-08-10  6  8
2020-08-11  9  6
2020-08-12  1  6
2020-08-13  5  7
  • 数据帧后面可能跟着一些其他代码,这些代码会产生错误或不会产生所需的结果

在 Stack Overflow 上提问时应提供的内容。

请勿添加您的数据作为此问题的答案。

最佳答案

第一:请勿发布数据图像,仅发布文本

第二:不要将数据粘贴到评论部分或作为答案,而是编辑您的问题

<小时/>

如何快速提供 pandas DataFrame 中的示例数据

  • 回答这个问题的方法不止一种。然而,这个答案并不意味着是一个详尽的解决方案。它提供了最简单的方法。
  • 对于好奇的人,Stack Overflow 上还提供了其他更详细的解决方案。
  1. 提供可共享数据集的链接(可能位于 GitHub 上或 Google 上的共享文件)。如果数据集很大并且目标是优化某些方法,这尤其有用。缺点是数据将来可能不再可用,从而降低了帖子的 yield 。
    • 问题中必须提供数据,但可以附上指向更广泛数据集的链接。
    • 不要仅发布数据的链接或图像。
  2. 提供 df.head(10).to_clipboard(sep=',', index=True) 的输出

代码:

提供pandas.DataFrame.to_clipboard的输出

df.head(10).to_clipboard(sep=',', index=True)
  • 如果您有多索引 DataFrame,请添加注释,说明哪些列是索引。
  • 注意:执行上一行代码时,不会出现任何输出。
    • 代码的结果现在位于剪贴板上。
  • 将剪贴板粘贴到您的 Stack Overflow 问题中的代码块
,a,b
2020-07-30,2,4
2020-07-31,1,5
2020-08-01,2,2
2020-08-02,9,8
2020-08-03,4,0
2020-08-04,3,3
2020-08-05,7,7
2020-08-06,7,0
2020-08-07,8,4
2020-08-08,3,2
  • 试图回答您问题的人可以将其复制到剪贴板,然后添加:
df = pd.read_clipboard(sep=',')

.head(10) 之外的数据帧的位置

  • 使用 .iloc 指定数据帧的一部分属性(property)
  • 以下示例选择第 3 - 11 行和所有列
df.iloc[3:12, :].to_clipboard(sep=',')

pd.read_clipboard的其他引用

Google Colab 用户

  • .to_clipboard() 不起作用
  • 使用.to_dict()复制您的数据框
# if you have a datetime column, convert it to a str
df['date'] = df['date'].astype('str')

# if you have a datetime index, convert it to a str
df.index = df.index.astype('str')

# output to a dict
df.head(10).to_dict(orient='index')

# which will look like
{'2020-07-30': {'a': 2, 'b': 4},
 '2020-07-31': {'a': 1, 'b': 5},
 '2020-08-01': {'a': 2, 'b': 2},
 '2020-08-02': {'a': 9, 'b': 8},
 '2020-08-03': {'a': 4, 'b': 0},
 '2020-08-04': {'a': 3, 'b': 3},
 '2020-08-05': {'a': 7, 'b': 7},
 '2020-08-06': {'a': 7, 'b': 0},
 '2020-08-07': {'a': 8, 'b': 4},
 '2020-08-08': {'a': 3, 'b': 2}}

# copy the previous dict and paste into a code block on SO
# the dict can be converted to a dataframe with 
# df = pd.DataFrame.from_dict(d, orient='index')  # d is the name of the dict
# convert datatime column or index back to datetime

关于python - 如何使用 to_clipboard() 提供 DataFrame 的可复制副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52413246/

相关文章:

python - 返回相似作者的列表

python - 将单独的 Pandas 数据框绘制为具有共享 x 轴的条形图

python,pandas 进行分组 value_count()

python - 按月和年对数据框列进行排序

python - 垂直投影和水平投影

python - Pandas 左外连接排除

python - 使用 python ('NoneType' 对象进行网页抓取没有属性 'get_text' )

python-3.x - 无法在 BigQuery 中使用 DML 语句在作业中设置目标表

python - 如何根据特定的列组向 DataFrame 添加额外的总和列?