在pandas

时间:2017-05-03 10:02:40

标签: python pandas

我用pandas读了一个csv数据,现在我想改变数据集的布局。我的excel数据集如下所示:

enter image description here

我使用df = pd.read_csv(Location2)

运行代码

这就是我得到的:

enter image description here

我想为timeWatt及其值设置一个单独的列。

我查看了文档,但是找不到能让它工作的东西。

3 个答案:

答案 0 :(得分:0)

使用read_excel

df = pd.read_excel(Location2)

答案 1 :(得分:0)

似乎您需要设置分隔两个字段的正确分隔符。尝试将delimiter=";"添加到参数

答案 2 :(得分:0)

我认为您需要read_csv中的参数sep,因为默认分隔符为,

df = pd.read_csv(Location2, sep=';')

样品:

import pandas as pd
from pandas.compat import StringIO

temp=u"""time;Watt
0;00:00:00;50
1;01:00:00;45
2;02:00:00;40
3;00:03:00;35"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";")
print (df)
       time  Watt
0  00:00:00    50
1  01:00:00    45
2  02:00:00    40
3  00:03:00    35

然后可以转换timeto_timedelta

df['time'] = pd.to_timedelta(df['time'])
print (df)
      time  Watt
0 00:00:00    50
1 01:00:00    45
2 02:00:00    40
3 00:03:00    35

print (df.dtypes)
time    timedelta64[ns]
Watt              int64
dtype: object