第一:不要发布图片资料,只请文字
第二:不要在评论部分粘贴数据,或者作为答案,而是编辑你的问题
如何快速提供数据帧中的样本数据
-
回答这个问题的方法不止一种。然而,这个答案并不是一个详尽的解决方案。它提供了最简单的方法。
-
-
提供到可共享数据集的链接(可能在GitHub上,也可能在Google上提供共享文件)。如果它是一个大的数据集,并且目标是优化某些方法,那么这一点特别有用。缺点是这些数据在将来可能不再可用,这降低了该职位的效益。
-
提供输出
df.head(10).to_clipboard(sep=',', index=True)
代码:
提供输出
pandas.DataFrame.to_clipboard
df.head(10).to_clipboard(sep=',', index=True)
-
-
注意
:执行前一行代码时,不会出现输出。
-
将剪贴板粘贴到
code block
在堆栈溢出问题中
,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
# 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