Python和groupby

2024-05-14 00:02:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我重新提出了我的问题。你知道吗

我有一个熊猫数据框,看起来像这样:

   data = [[0, 'cat1', 1, 11], [33, 'cat1', 3, 52], [0, 'cat1', 4, 14], [11, 'cat2', 3, 22], [14, 'cat2', 2, 18], [0, 'cat2', 5, 13]]
   df = pd.DataFrame.from_records(data_str, index=['time1', 'time2', 'time2', 'time3', 'time1', 'time1'], columns = ['Text Time',  'Tag', 'Relevance', 'Text length'])
   df = df.sort_index()
   df.plot(x='Text Time', y = 'Relevance')



Text       Time   Tag  Relevance  Text length
time1          0  cat1          1           11
time1         33  cat1          3           52
time2         85  cat1          4           14
time1         11  cat2          3           22
time2         99  cat2          2           18
time3        117  cat2          5           13

Text Time列是我的时间,x轴,相关性是y轴。 现在我想:

  • 在x轴上,用索引(time1time2time3)对Text Time进行分组
  • 我只想绘制两行,cat1cat2,每一行都由相关数据表示。你知道吗

enter image description here


Tags: 数据textdfdataindextimetaglength
1条回答
网友
1楼 · 发布于 2024-05-14 00:02:36

因为你改变了问题,这里是更新的答案:

请参见代码中的注释

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
%matplotlib inline

# read your dataframe and sort
df = pd.read_clipboard()
df.drop(columns=['length'], inplace=True)
df.rename(columns={'Text.1': 'Text length'}, inplace=True)
df.sort_values(['Text', 'Tag', 'Time'], inplace=True)
x = list(df['Time']) # set x axis by creating a list of time

fig, ax = plt.subplots() # plot mulitple lines
for xlabels, group in df.groupby(['Tag']): # group by Tag
    df['Time'] = df['Time'].astype(str) # change time to a string to create xticks
    xticks = list(df['Time']+'\n'+df['Text']+'\n'+df['Tag']) # create xticks

    group.plot(kind='line',x='Time', y='Relevance', ax=ax)
    ax.legend(['Cat1', 'Cat2'])
    ax.set_xlabel('Time')
    plt.xticks(x, xticks)

    # resize plot
    pos1 = ax.get_position()
    pos2 = [pos1.x0, pos1.y0,  pos1.width + 1, pos1.height + .5]
    ax.set_position(pos2)

output

相关问题 更多 >