如何调用pandas read_csv()而不解析日期字符串

时间:2016-11-26 02:32:18

标签: python pandas

我正在使用以csv格式从网上下载的一些数据。原始数据如下所示。

logo

我使用以下代码来阅读它

Test Data
"Date","T1","T2","T3","T4","T5","T6","T7","T8"
"105/11/01","123,855","1,150,909","9.30","9.36","9.27","9.28","-0.06","60",
"105/11/02","114,385","1,062,118","9.26","9.42","9.23","9.31","+0.03","78",
"105/11/03","71,350","659,848","9.30","9.30","9.20","9.28","-0.03","42",

我也尝试过使用

import pandas as pd
df = pd.read_csv("test.csv", skiprows=[0], usecols=[0,3,4,5])

我总是得到以下结果

import pandas as pd
df = pd.read_csv("test.csv", skiprows=[0], usecols=[0,3,4,5], keep_date_col=True)

这就是我想要的

           Date    T3    T4   T5
105/11/01  9.30  9.36  9.27  NaN
105/11/02  9.26  9.42  9.23  NaN
105/11/03  9.30  9.30  9.20  NaN

正如您所看到的那样,pandas将日期字符串视为数据的一部分,并将索引转移到左侧的一列,这导致最后一列为 Date T3 T4 T5 105/11/01 9.30 9.36 9.27 105/11/02 9.26 9.42 9.23 105/11/03 9.30 9.30 9.20

我已经阅读了read_csv()上的pandas文档,发现它可以使用NaNparse_dates参数解析日期,但有没有办法解析日期,就像现在这样做?

1 个答案:

答案 0 :(得分:2)

这似乎运作良好:

index_col : int or sequence or False, default None
    Column to use as the row labels of the DataFrame. If a sequence is given, a
    MultiIndex is used. If you have a malformed file with delimiters at the end
    of each line, you might consider index_col=False to force pandas to _not_
    use the first column as the index (row names)

这也来自帮助文档:

function setExerciseData(exerciseName, value) {
  return {
    type: 'SET_EXERCISE_DATA',
    exerciseName,
    value,
  }
}

export function gatherFormData(exerciseName) {
  return (dispatch, getState) => {
    const form = getState().form;
    const value = form.workout.values[exerciseName];
    dispatch(setExerciseData(exerciseName, value))
  }
}