所以我用Pandas读了一个* .csv文件,但如果我想绘制它,它会显示以下错误,
ValueError: could not convert string to float: '23:00:00'
我使用
阅读并打印以下* .csv文件df1 = pd.read_csv(Location_l, sep=";")
df1
我试图改变时间'到时间,但它仍然没有绘制它。
如何转换' time'为了绘制df1?
谢谢!
答案 0 :(得分:0)
如果想要绘制timedelta
,现在它是not implemented,那么可能的解决方案是将time
列转换为total_seconds
:
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
converter = {'time':lambda x: pd.to_timedelta(x)}
df = pd.read_csv(StringIO(temp), sep=";", converters=converter)
df['time_in_sec'] = df['time'].dt.total_seconds()
print (df)
time Watt time_in_sec
0 00:00:00 0 0.0
1 01:00:00 0 3600.0
2 02:00:00 0 7200.0
3 03:00:00 0 10800.0
4 04:00:00 0 14400.0
5 05:00:00 45 18000.0
6 06:00:00 56 21600.0
7 07:00:00 88 25200.0
df.plot(x='time_in_sec', y='Watt')
另一种解决方案:
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";")
df['time_in_sec'] = pd.to_timedelta(df['time']).dt.total_seconds()
print (df)
time Watt time_in_sec
0 00:00:00 0 0.0
1 01:00:00 0 3600.0
2 02:00:00 0 7200.0
3 03:00:00 0 10800.0
4 04:00:00 0 14400.0
5 05:00:00 45 18000.0
6 06:00:00 56 21600.0
7 07:00:00 88 25200.0
df.plot(x='time_in_sec', y='Watt')
在read_csv
中将timedelta
解析为datetime
,然后转换为time
的解决方案:
import pandas as pd
import numpy as np
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", parse_dates=['time'])
df['time'] = df['time'].dt.time
print (df)
time Watt
0 00:00:00 0
1 01:00:00 0
2 02:00:00 0
3 03:00:00 0
4 04:00:00 0
5 05:00:00 45
6 06:00:00 56
7 07:00:00 88
df.plot(x='time', y='Watt')
另一种可能的解决方案是在read_csv
中设置索引:
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", index_col=['time'])
df.index = pd.to_timedelta(df.index).total_seconds()
print (df)
Watt
time
0.0 0
3600.0 0
7200.0 0
10800.0 0
14400.0 0
18000.0 45
21600.0 56
25200.0 88
df['Watt'].plot()
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", parse_dates=True, index_col=['time'])
df.index = df.index.time
print (df)
Watt
00:00:00 0
01:00:00 0
02:00:00 0
03:00:00 0
04:00:00 0
05:00:00 45
06:00:00 56
07:00:00 88
df['Watt'].plot()
答案 1 :(得分:0)
可能是因为您的数据框中有null或N / A数据 你想使用" df2.fillna(0)"