df.head(10).to_clipboard(sep=',', index=True)
Provide the output of pandas.DataFrame.to_clipboard
df.head(10).to_clipboard(sep=',', index=True)
code block
in your Stack Overflow question,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)
df.iloc[3:12, :].to_clipboard(sep=',')
pd.read_clipboard
.to_clipboard()
won't work# 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()
if you do something like print(df.head(20))
and paste the output in code format, then we can use pd.read_clipboard()
to load the data into a dataframe. This approach works for the vast majority of questions posted under the pandas
tag but fails miserably for questions involving multiindex