簡體   English   中英

將多列組合成唯一標識符以分隔繪圖數據

[英]Combine multiple columns into a unique identifier to separate plot data

我有一個大約 1000 個推文 ID 的 Pandas df,它們的生命周期以秒為單位(生命周期是第一次和最后一次轉發之間的時間距離)。 下面是我的 df 的一個子集的頭部:

推文ID 壽命(時間增量) 壽命(小時) 類型1 類型2 類型3 類型4
329664 0 天 05:27:22 5.456111 1 0 0 0
722624 0 天 12:43:43 12.728611 1 1 0 0
866498 2天 09:00:28 57.007778 0 1 1 0
156801 0 天 03:01:29 3.024722 1 0 0 0
941440 0 天 06:39:58 6.666111 0 1 1 1

注意1:推文的生命周期顯示在兩列中(列具有不同的數據類型):

  1. lifetime(timedelta)以 timedelta64[ns] 格式顯示推文生命周期,
  2. lifetime(hours)以小時為單位顯示推文生命周期(float64 類型)。 我通過使用以下方法從生命周期(timedelta)列中提取小時數來創建第 2 列: df['lifetime_hours'] = df['lifetime(timedelta)'] / np.timedelta64(1, 'h')

注意2:一條推文可以屬於多個類型。 例如,tweet id:329664 只是 type1,而 tweet id:722624 是 type1 和 type2。

我想繪制不同類型推文的推文壽命分布。 我可以繪制推文的生命周期分布如下(對於所有推文): 這是條形圖: 在此處輸入圖像描述

這是情節: 在此處輸入圖像描述

這是我如何創建上述圖(例如,條形圖):

bins = range(0, df['lifetime_hours'].max().astype(int), 3) 
data = pd.cut(df['lifetime_hours'], bins, include_lowest=True)

from matplotlib.pyplot import figure
plt.figure(figsize=(20,4))

data.value_counts().sort_index().plot(kind='bar')

plt.xlabel('Tweets Lifetime(hours)')
plt.ylabel('Number of Tweets Active')
plt.title('Distribution of Tweets lifetime')

我的問題是:如何在一個情節中繪制兩種類型的推文的生命周期分布?

有人可以幫我嗎?

  • 為了按類型分隔數據,應該有一個標識符列。
    • 這可以通過將01列值乘以列類型名稱來創建,然后將列值連接到單個字符串中作為新列。
  • python 3.10pandas 1.4.2matplotlib 3.5.1seaborn 0.11.2

導入和數據框

import pandas as pd
import numpy as np
import seaborn as sns

# start data
data = {'tweet_id': [329664, 722624, 866498, 156801, 941440],
        'lifetime(timedelta)': [pd.Timedelta('0 days 05:27:22'), pd.Timedelta('0 days 12:43:43'), pd.Timedelta('2 days 09:00:28'),
                                pd.Timedelta('0 days 03:01:29'), pd.Timedelta('0 days 06:39:58')],
        'type1': [1, 1, 0, 1, 0], 'type2': [0, 1, 1, 0, 1], 'type3': [0, 0, 1, 0, 1], 'type4': [0, 0, 0, 0, 1]}
df = pd.DataFrame(data)

# insert hours columns
df.insert(loc=2, column='lifetime(hours)', value=df['lifetime(timedelta)'].div(pd.Timedelta('1 hour')))

# there can be 15 combinations of types for the 4 type columns
# it's best to rename the columns for ease of use
# rename the type columns; can also use df.rename(...)
cols = ['T1', 'T2', 'T3', 'T4']
df.columns = df.columns[:3].tolist() + cols

# create a new column as a unique identifier for types
types = df[cols].mul(cols).replace('', np.nan).dropna(how='all')
df['Types'] = types.apply(lambda row: ' '.join(row.dropna()), axis=1)

# create a column for the bins
bins = range(0, df['lifetime(hours)'].astype(int).add(4).max(), 3) 
df['Tweets Liftime(hours)'] = pd.cut(df['lifetime(hours)'], bins, include_lowest=True)

# display(df)
   tweet_id lifetime(timedelta)  lifetime(hours)  T1  T2  T3  T4     Types Tweets Liftime(hours)
0    329664     0 days 05:27:22         5.456111   1   0   0   0        T1            (3.0, 6.0]
1    722624     0 days 12:43:43        12.728611   1   1   0   0     T1 T2          (12.0, 15.0]
2    866498     2 days 09:00:28        57.007778   0   1   1   0     T2 T3          (57.0, 60.0]
3    156801     0 days 03:01:29         3.024722   1   0   0   0        T1            (3.0, 6.0]
4    941440     0 days 06:39:58         6.666111   0   1   1   1  T2 T3 T4            (6.0, 9.0]

創建頻率表

ct = pd.crosstab(df['Tweets Liftime(hours)'], df['Types'])

# display(ct)
Types                  T1  T1 T2  T2 T3  T2 T3 T4
Tweets Liftime(hours)                            
(3.0, 6.0]              2      0      0         0
(6.0, 9.0]              0      0      0         1
(12.0, 15.0]            0      1      0         0
(57.0, 60.0]            0      0      1         0

陰謀

pandas.DataFrame.plot

  • 用途ct
ax = ct.plot(kind='bar', figsize=(20, 5), width=0.1, rot=0)
ax.set(ylabel='Number of Tweets Active', title='Distribution of Tweets Lifetime')
ax.legend(title='Types', bbox_to_anchor=(1, 1), loc='upper left')

在此處輸入圖像描述

seaborn.catplot

  • 使用df無需重塑
p = sns.catplot(kind='count', data=df, x='Tweets Liftime(hours)', height=4, aspect=4, hue='Types')
p.set_xticklabels(rotation=45)
p.fig.subplots_adjust(top=0.9)
p.fig.suptitle('Distribution of Tweets Lifetime')
p.axes[0, 0].set_ylabel('Number of Tweets Active')

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM