gpt4 book ai didi

python - pandas.read_csv 读取非常原始的 CSV 文件(前三个数字编译成 datetime.datetime 对象)的正确设置是什么?

转载 作者:行者123 更新时间:2023-12-01 03:08:57 24 4
gpt4 key购买 nike

更明确地说,我有这种类型的数据

2011,1,14, Orange  ,Buy,1500,
2011,1,19, Apple ,Sell,1500,
2011,1,19, Banana ,Buy,4000,

我试图将其直接读入 DataFrame,其 columns=['date','fruit', 'trade','quantity'] 形状如下。

print df_grocer.date
2011-01-14 16:00:00
2011-01-19 16:00:00
2011-01-19 16:00:00

print df_grocer.fruit
Orange
Apple
Banana

提前致谢

最佳答案

pd.read_csv

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’

Note: A fast-path exists for iso8601-formatted dates.

from io import StringIO
import pandas as pd

txt = """2011,1,14, Orange ,Buy,1500,
2011,1,19, Apple ,Sell,1500,
2011,1,19, Banana ,Buy,4000,"""

df = pd.read_csv(
StringIO(txt),
skipinitialspace=True,
header=None,
parse_dates=dict(date=[0, 1, 2]),
usecols=[0, 1, 2, 3, 4, 5],
names=['_', '_', '_', 'fruit', 'trade', 'quantity']
)


print(df)

date fruit trade quantity
0 2011-01-14 Orange Buy 1500
1 2011-01-19 Apple Sell 1500
2 2011-01-19 Banana Buy 4000

关于python - pandas.read_csv 读取非常原始的 CSV 文件(前三个数字编译成 datetime.datetime 对象)的正确设置是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43082314/

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