Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何与seaborn兼容使用熊猫数据帧或系列?_Python 3.x_Pandas_Dataframe_Time Series_Seaborn - Fatal编程技术网

Python 3.x 如何与seaborn兼容使用熊猫数据帧或系列?

Python 3.x 如何与seaborn兼容使用熊猫数据帧或系列?,python-3.x,pandas,dataframe,time-series,seaborn,Python 3.x,Pandas,Dataframe,Time Series,Seaborn,我导入它并选择一个间隔 data\u frame=pd.read\u csv('househouse\u power\u consumpion.txt', sep=';', parse_dates={'dt':['Date','Time']}, 推断日期时间格式=真, 内存不足=错误, na_值=['nan','?'], 索引(col='dt') df_08_09=数据帧截断(在='2008-01-01'之前,在='2010-01-01'之后) df_08_09.info() 得到 <c

我导入它并选择一个间隔

data\u frame=pd.read\u csv('househouse\u power\u consumpion.txt',
sep=';',
parse_dates={'dt':['Date','Time']},
推断日期时间格式=真,
内存不足=错误,
na_值=['nan','?'],
索引(col='dt')
df_08_09=数据帧截断(在='2008-01-01'之前,在='2010-01-01'之后)
df_08_09.info()
得到

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1052641 entries, 2008-01-01 00:00:00 to 2010-01-01 00:00:00
Data columns (total 7 columns):
Global_active_power      1052641 non-null float64
Global_reactive_power    1052641 non-null float64
Voltage                  1052641 non-null float64
Global_intensity         1052641 non-null float64
Sub_metering_1           1052641 non-null float64
Sub_metering_2           1052641 non-null float64
Sub_metering_3           1052641 non-null float64
dtypes: float64(7)
memory usage: 64.2 MB
dt
总是出问题,因为seaborn由于某种原因无法访问它。我试图访问DatetimeIndex,但我发现没有办法提取它并使其成为数据列,因为我不太习惯使用它

我希望seaborn在数据中找到
dt
,但它没有找到,并相应地抛出一个错误。我很清楚地看到了这一点,但idk如何以一种有效的python/pandas/seaborn方式处理这一问题。所以请帮帮我!:)


…顺便说一句,还有一个问题。。。我还想知道为什么
df\u 08\u 09.Global\u active\u power.values
返回的是
(n,)
形状的np.array而不是
(n,1)
。我总是被迫执行
values=np.array([values]).transpose()
来恢复
(n,1)

您可以使用一种变通方法,首先将日期时间列转换为整数,然后用日期时间值替换matplotlib的轴刻度标签,例如

import pandas as pd
import numpy as np
import seaborn
from datetime import datetime


data_frame = pd.read_csv('household_power_consumption.txt', 
                 sep=';', 
                 parse_dates={'dt' : ['Date', 'Time']}, 
                 infer_datetime_format=True, 
                 low_memory=False, 
                 na_values=['nan','?']) 
                 #index_col='dt') No need for this, as we are not working with indexes
df_08_09 = data_frame.truncate(before='2008-01-01', after='2010-01-01')

# Convert to integers
df_08_09['date_ordinal'] = pd.to_datetime(df_08_09['dt']).apply(lambda date: date.timestamp())

# Plotting as integers
ax = seaborn.regplot(data=df_08_09, x="date_ordinal", y="Global_active_power")

# Adjust axis
ax.set_xlim(df_08_09['date_ordinal'].min() - 1, df_08_09['date_ordinal'].max() + 1)
ax.set_ylim(0, df_08_09['Global_active_power'].max() + 1)

# Set x-axis-tick-labels to datetime
new_labels = [datetime.utcfromtimestamp(int(item)) for item in ax.get_xticks()]
ax.set_xticklabels(new_labels, rotation = 45)


参考资料:

截断不适用于您的解决方案。我得到了2010-01-01之后的数据。因此,需要执行使用
index\u col='dt'
的索引,以便最终得到冗余的数据列。事实上,我认为会有一些更优雅的东西,指向索引列,但作为一个“快速而肮脏”的解决方案是可以接受的