如何使用 to_clipboard() 提供 DataFrame 的可复制副本 [英] How to provide a reproducible copy of your DataFrame with to_clipboard()

查看:22
本文介绍了如何使用 to_clipboard() 提供 DataFrame 的可复制副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 此问题之前被标记为与如何制作可重现的好的熊猫的重复例子.
    • 如果您需要制作合成(假)数据来共享,请转到那个问题.
    • 其他问题和相关答案涵盖了如何创建可重现的数据框.
    • 它们不包括如何复制带有.to_clipboard,而这个问题专门针对 .to_clipboard.
    • This question was previously marked as a duplicate of How to make good reproducible pandas examples.
      • Go to that question if you need to make synthetic (fake) data to share.
      • The other question and associated answers cover how to create a reproducible dataframe.
      • They do not cover how to copy an existing dataframe with .to_clipboard, while this question specifically covers .to_clipboard.
      • 这似乎是一个显而易见的问题.但是,许多询问 Pandas 问题的用户都是新手且缺乏经验.
      • 提出问题的一个关键组成部分是如何创建最小、完整和可验证的示例,它解释了什么";和为什么",而不是如何".
      • This may seem like an obvious question. However, many of the users asking questions about Pandas are new and inexperienced.
      • A critical component of asking a question is How to create a Minimal, Complete, and Verifiable example, which explains "what" and "why", but not "how".
      • 在本例中,我创建了合成数据,这是创建可重现数据集的一个选项,但不在本问题的范围内.
      • 想一想,就好像您加载了一个文件,只需共享其中的一小部分即可重现错误.
      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
      

      • 数据帧后面可能会跟一些其他代码,这些代码会产生错误或不会产生预期的结果
      • 推荐答案

        第一:不要张贴数据图片,请只发文字

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


        如何快速提供来自 Pandas DataFrame 的样本数据

        • 回答这个问题的方法不止一种.但是,这个答案并不意味着是一个详尽的解决方案.它提供了最简单的方法.
        • 对于好奇的人,Stack Overflow 上还提供了其他更详细的解决方案.
          1. 提供指向可共享数据集的链接(可能位于 GitHub 上或 Google 上的共享文件).如果它是一个大型数据集并且目标是优化某些方法,这将特别有用.缺点是数据可能在未来不再可用,从而降低了帖子的收益.
            • 问题中必须提供数据,但可以附上指向更广泛数据集的链接.
            • 不要只发布数据的链接或图片.

          代码:

          提供pandas.DataFrame.to_clipboard

          df.head(10).to_clipboard(sep=',', index=True)
          

          • 如果您有一个多索引 DataFrame,请添加注释,说明哪些列是索引.
          • 注意:执行上一行代码时,不会出现任何输出.
            • 代码的结果现在在剪贴板上.
              • If you have a multi-index DataFrame add a note, telling which columns are the indices.
              • Note: when the previous line of code is executed, no output will appear.
                • The result of the code is now on the clipboard.
                • ,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 属性
                    • 以下示例选择第 3 - 11 行和所有列
                    • df.iloc[3:12, :].to_clipboard(sep=',')
                      

                      pd.read_clipboard 的附加参考

                      • 使用 pd.read_clipboard 指定多级列?
                      • 使用 pd.read_clipboard 时如何处理包含空格的列名?
                      • 在使用 pd.read_clipboard 复制数据帧时如何处理自定义命名索引?
                      • Additional References for pd.read_clipboard

                        • Specify Multi-Level columns using pd.read_clipboard?
                        • How do you handle column names having spaces in them when using pd.read_clipboard?
                        • How to handle custom named index when copying a dataframe using pd.read_clipboard?
                          • .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
                          

                          • 使用 .to_dict() 获得更彻底的答案
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆