私信  •  关注

Horbaje

Horbaje 最近创建的主题
Horbaje 最近回复了
7 年前
回复了 Horbaje 创建的主题 » python数据帧中的部分转换列

您可以执行lstrip来删除不需要的零。

df['ABCD'] = df['ABCD'].map(lambda x: x.lstrip('0'))

之后,您将能够执行groub,而无需转换列的类型。

7 年前
回复了 Horbaje 创建的主题 » 如何在python中为dataframe列中的唯一条目绘制条形图?

这是我可以建议您的代码:

#Read your Dataframe
df = pd.read_csv('Test.csv', index_col=None, header='infer', encoding="utf-8-sig")

#split the time_window column into two columns, so you can calculate Duration
df['start_time'], df['end_time'] = df['time_window'].str.split('to', 1).str

#convert start and ending time columns to datetime and ID to numeric
df[['start_time','end_time']] = df[['start_time','end_time']].apply(pd.to_datetime, format='%Y-%m-%d %H:%M:%S')
df["ID"] = pd.to_numeric(df["ID"])

#Calculate the duration of a time window and convert into seconds
df['Duration'] = df['start_time'] - df['end_time']
df['Duration']=df['Duration']/np.timedelta64(1,'s')

#plot 
ax = df.plot(x="ID", y=["min_time", "max_time", "mean_time"], kind="bar", rot=25)
ax.set_xlabel("Instances (ID)")
ax.set_ylabel("Duraction(s)")
ax.set_title("Visualization")

rects = ax.patches
labels = df['Duration']

for rect, label in zip(rects, labels):
     height = rect.get_height()
     ax.text(rect.get_x() + rect.get_width(), height+0.3, label,
             ha='center', va='bottom')

这将生成以下数据帧和图。

enter image description here

enter image description here

这就是你要找的吗?你说你不想要子块,但听起来你也希望每个id都有一个单独的图表?