gpt4 book ai didi

python - Pandas 从剪贴板读取带有日期时间列的 DataFrame

转载 作者:太空狗 更新时间:2023-10-30 00:01:18 25 4
gpt4 key购买 nike

我看到很多发布到 StackOverflow 的 DataFrames 看起来像:

          a                  dt         b
0 -0.713356 2015-10-01 00:00:00 -0.159170
1 -1.636397 2015-10-01 00:30:00 -1.038110
2 -1.390117 2015-10-01 01:00:00 -1.124016

我还没有想出使用.read_clipboard 将这些复制到我的解释器中的好方法| (.read_table docs 中的参数列表)。

我认为关键是 parse_dates 参数:

parse_dates : boolean or list of ints or names or list of lists or dict, default False
* boolean. If True -> try parsing the index.
* list of ints or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.
* list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.
* dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’

pd.read_clipboard(parse_dates={'dt': [1, 2]}) 引发异常 NotImplementedError: file structure not yet supported

当我尝试跳过第一行时 pd.read_clipboard(parse_dates=[[1, 2]], names=['a', 'dt1', 'dt2', 'b'], skiprows=1 , header=None) 我得到了同样的异常。

其他人是怎么做到的?

最佳答案

这就是我的工作。首先,确保您的列之间有两个空格:

          a                  dt         b
0 -0.713356 2015-10-01 00:00:00 -0.159170
1 -1.636397 2015-10-01 00:30:00 -1.038110
2 -1.390117 2015-10-01 01:00:00 -1.124016

注意,日期时间列在日期和时间之间有一个空格。这个很重要。接下来,我使用类似这样的东西将其加载到:

df = pd.read_clipboard(sep='\s{2,}', parse_dates=[1], engine='python')
df

a dt b
0 0 -0.713356 2015-10-01 00:00:00 -0.159170
1 1 -1.636397 2015-10-01 00:30:00 -1.038110
2 2 -1.390117 2015-10-01 01:00:00 -1.124016

df.dtypes

a object
dt datetime64[ns]
b float64
dtype: object

是的,这不是一个完全自动化的过程,但只要您处理的是要复制的小数据帧,就那么不好。尽管我愿意看到更好的选择。

关于python - Pandas 从剪贴板读取带有日期时间列的 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48449355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com