gpt4 book ai didi

python - pd.read_csv 解析不正确

转载 作者:行者123 更新时间:2023-12-01 09:02:48 25 4
gpt4 key购买 nike

我有一个如下所示的 CSV 文件:

Date,Time,Mood,Tags,Medications,Notes
"Jul 25, 2018",9:41 PM,8,,,"",
"Jul 26, 2018",10:05 AM,4,,,"",
"Jul 26, 2018",12:00 PM,3,,,"",
"Jul 26, 2018",7:00 PM,8,,,"",
"Jul 27, 2018",12:01 PM,8,,,"",

我运行以下代码:

import pandas as pd

df = pd.read_csv("./data/MoodLog_2018_09_14.csv",
dtype={'Date': str, 'Time': str, 'Mood': str, 'Tags': str,
'Medications': str, 'Notes': str})

print(df['Time'].head(5))

它打印以下内容:

Jul 25, 2018    8
Jul 26, 2018 4
Jul 26, 2018 3
Jul 26, 2018 8
Jul 27, 2018 8
Name: Time, dtype: object

它在时间列中包含心情列。

这是为什么?

最佳答案

问题在于您的行有尾随 ,,而标题没有。将标题更改为:日期、时间、心情、标签、药物、注释,您将获得一个额外的列,然后可以将其删除。

输入:test.csv

Date,Time,Mood,Tags,Medications,Notes,
"Jul 25, 2018",9:41 PM,8,,,"",
"Jul 26, 2018",10:05 AM,4,,,"",
"Jul 26, 2018",12:00 PM,3,,,"",
"Jul 26, 2018",7:00 PM,8,,,"",
"Jul 27, 2018",12:01 PM,8,,,"",

代码:

df = pd.read_csv("test.csv", 
dtype={'Date': str, 'Time': str, 'Mood': str, 'Tags': str,
'Medications': str, 'Notes': str}).iloc[:, :-1]

输出:df

           Date      Time Mood Tags Medications Notes
0 Jul 25, 2018 9:41 PM 8 NaN NaN NaN
1 Jul 26, 2018 10:05 AM 4 NaN NaN NaN
2 Jul 26, 2018 12:00 PM 3 NaN NaN NaN
3 Jul 26, 2018 7:00 PM 8 NaN NaN NaN
4 Jul 27, 2018 12:01 PM 8 NaN NaN NaN

关于python - pd.read_csv 解析不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52335593/

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