本文介绍了如何使用to_clipboard()提供DataFrame的可复制副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 2018-09-18_reproducible_dataframe.ipynb 该问题以前被标记为如何制作良好的可复制熊猫实例。 如果需要共享合成(伪)数据,请转到该问题。 其他问题和相关答案涵盖了如何创建可重现的数据框。 它们不涉及如何复制具有 .to_clipboard ,而这个问题专门涵盖了 .to_clipboard 。 这似乎是一个显而易见的问题。但是,许多询问有关熊猫的问题的用户都是新手,没有经验。 提出问题的关键因素是如何创建最小,完整和可验证的示例,该示例解释了什么和为什么,而不是如何。 例如,作为OP ,我可能有以下数据框: 在此示例中,我创建了合成数据,这是创建可重现数据集的一种选择,但不在此问题范围内。 考虑到这一点,就好像您已经加载了文件,并且只需要共享其中的一部分即可重现错误。 将pandas作为pd 导入numpy作为np 从datetime导入datetime 从字符串import ascii_lowercase as al np.random.seed(365)行= 15 cols = 2 数据= np.random.randint(0,10,size =(rows,cols )) index = pd.bdate_range(datetime.today(),freq ='d',period = rows) df = pd.DataFrame(data = data,index = index, column = list(al [:cols])) ab 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 该数据框后面可能跟随其他一些代码,这些代码会产生错误或无法产生预期的结果 询问时应提供的内容一个关于堆栈溢出的问题。 一个写得很好的连贯问题-作为文本 产生错误的代码-为文本 整个错误回溯-为文本 可能,当前&预期结果-为文本或图像(如果是绘图的话) 数据,采用易于使用的形式-为文字 请勿添加数据作为此问题的答案。 解决方案 首先:不要添加您的数据作为问题的答案 如何从熊猫DataFrame快速提供示例数据 有多种方法可以回答这个问题。但是,此答案并不代表是详尽的解决方案。它提供了最简单的方法。 出于好奇,Stack Overflow还提供了其他更详细的解决方案。 提供指向可共享数据集的链接(可能在GitHub或Google上的共享文件)。如果数据集很大且目标是优化某些方法,则此功能特别有用。缺点是数据将来可能不再可用,从而降低了发布的好处。 问题中必须提供数据,但可以附带指向更广泛的数据集的链接。 不要仅发布数据的链接或图像。 提供 df的输出.head(10).to_clipboard(sep =',',index = True) 代码: 提供 pandas.DataFrame.to_clipboard df.head(10).to_clipboard(sep =',',index = True) 如果您有多索引DataFrame,请添加注释,告诉索引是哪些列。 注意:执行上一行代码时,将不会显示任何输出。 代码的结果现在在剪贴板上。 将剪贴板粘贴到堆栈溢出问题中的代码块 ,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) $ b $以外的数据框位置b 使用 .iloc pr操作符 下面的示例选择第3-11行和所有列 df.iloc [3:12,:]。to_clipboard(sep =',') pd.read_clipboard 的其他参考 使用pd.read_clipboard指定多级列? 使用pd.read_clipboard时如何处理包含空格的列名? 使用pd.read_clipboard复制数据框时如何处理自定义命名索引? Google Colab用户 .to_clipboard()不起作用 使用 .to_dict() 复制数据框 #,如果您有日期时间列,将其转换为str df ['date'] = df ['date']。astype('str') #如果您有日期时间索引,请将其转换为str df.index = df.index.astype('str') #输出到字典 df.head(10).to_dict(orient ='index') #看起来像 {'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}} #复制先前的字典,并粘贴到代码块上,这样#可以使用将dict转换为数据框#df = pd.DataFrame.from_dict(d,orient ='index')#d是字典的名称#convert datatime列或返回日期时间的索引 使用 .to_dict() 如何高效地构建和共享 如何制作良好的可重复熊猫实例 li> 2018-09-18_reproducible_dataframe.ipynbThis 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.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".For example, as the OP, I may have the following dataframe:For this example, I've created synthetic data, which is an option for creating a reproducible dataset, but not within the scope of this question.Think of this, as if you've loaded a file, and only need to share a bit of it, to reproduce the error.import pandas as pdimport numpy as npfrom datetime import datetimefrom string import ascii_lowercase as alnp.random.seed(365)rows = 15cols = 2data = 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 b2020-07-30 2 42020-07-31 1 52020-08-01 2 22020-08-02 9 82020-08-03 4 02020-08-04 3 32020-08-05 7 72020-08-06 7 02020-08-07 8 42020-08-08 3 22020-08-09 6 22020-08-10 6 82020-08-11 9 62020-08-12 1 62020-08-13 5 7The dataframe could be followed by some other code, that produces an error or doesn't produce the desired outcomeThings that should be provided when asking a question on Stack Overflow.A well written coherent question - as textThe code that produces the error - as textThe entire error Traceback - as textPotentially, the current & expected outcome - as text or image, if it's a plotThe data, in an easily usable form - as textDo not add your data as an answer to this question. 解决方案 First: Do not add your data as an answer to the questionHow to quickly provide sample data from a pandas DataFrameThere is more than one way to answer this question. However, this answer isn't meant as an exhaustive solution. It provides the simplest method possible.For the curious, there are other more verbose solutions provided on Stack Overflow.Provide a link to a shareable dataset (maybe on GitHub or a shared file on Google). This is particularly useful if it's a large dataset and the objective is to optimize some method. The drawback is that the data may no longer be available in the future, which reduces the benefit of the post.Data must be provided in the question, but can be accompanied by a link to a more extensive dataset.Do not post only a link or an image of the data.Provide the output of df.head(10).to_clipboard(sep=',', index=True)Code:Provide the output of pandas.DataFrame.to_clipboarddf.head(10).to_clipboard(sep=',', index=True)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.Paste the clipboard into a code block in your Stack Overflow question,a,b2020-07-30,2,42020-07-31,1,52020-08-01,2,22020-08-02,9,82020-08-03,4,02020-08-04,3,32020-08-05,7,72020-08-06,7,02020-08-07,8,42020-08-08,3,2This can be copied to the clipboard by someone trying to answer your question, and followed by:df = pd.read_clipboard(sep=',')Locations of the dataframe other the .head(10)Specify a section of the dataframe with the .iloc propertyThe following example selects rows 3 - 11 and all the columnsdf.iloc[3:12, :].to_clipboard(sep=',')Additional References for pd.read_clipboardSpecify 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?Google Colab Users.to_clipboard() won't workUse .to_dict() to copy your dataframe# if you have a datetime column, convert it to a strdf['date'] = df['date'].astype('str')# if you have a datetime index, convert it to a strdf.index = df.index.astype('str')# output to a dictdf.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 datetimeFor a more thorough answer using .to_dict()How to efficiently build and share a sample dataframe?How to make good reproducible pandas examples 这篇关于如何使用to_clipboard()提供DataFrame的可复制副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 21:42