我用pandas读了一个csv数据,现在我想改变数据集的布局。我的excel数据集如下所示:
我使用df = pd.read_csv(Location2)
这就是我得到的:
我想为time
和Watt
及其值设置一个单独的列。
我查看了文档,但是找不到能让它工作的东西。
答案 0 :(得分:0)
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
然后可以转换time
列to_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