【问题标题】:Different sequence of names with pandas不同的名字顺序与熊猫
【发布时间】:2016-07-15 02:28:20
【问题描述】:

我有数据框

    used_at  common users                     pair of websites
0      2014          1364                   avito.ru and e1.ru
1      2014          1716                 avito.ru and drom.ru
2      2014          1602                 avito.ru and auto.ru
3      2014           299           avito.ru and avtomarket.ru
4      2014           579                   avito.ru and am.ru
5      2014           602             avito.ru and irr.ru/cars
6      2014           424       avito.ru and cars.mail.ru/sale
7      2014           634                    e1.ru and drom.ru
8      2014           475                    e1.ru and auto.ru
9      2014           139              e1.ru and avtomarket.ru
10     2014           224                      e1.ru and am.ru
11     2014           235                e1.ru and irr.ru/cars
12     2014           154          e1.ru and cars.mail.ru/sale
13     2014           874                  drom.ru and auto.ru
14     2014           247            drom.ru and avtomarket.ru
15     2014           394                    drom.ru and am.ru
....

当我写graph_by_common_users = common_users.pivot(index='pair of websites', columns='used_at', values='common users') 我明白了

used_at                                2014    2015
pair of websites                                   
am.ru and cars.mail.ru/sale           166.0     NaN
am.ru and irr.ru/cars                 223.0     NaN
auto.ru and am.ru                     408.0   224.0
auto.ru and avtomarket.ru             243.0   162.0
auto.ru and cars.mail.ru/sale         330.0   195.0
auto.ru and drom.ru                     NaN   799.0
auto.ru and irr.ru/cars               409.0   288.0
avito.ru and am.ru                    579.0   262.0
....

我有NaN,因为有些顺序不同。例如 我有2014 我有am.ru and cars.mail.ru/sale2015 我有cars.mail.ru/sale and am.ru。我该如何改变呢?

添加我的代码

import pandas as pd
import itertools
import matplotlib.pyplot as plt

df = pd.read_csv("avito_trend.csv", parse_dates=[2])


def f(df):
    dfs = []
    for x in [list(x) for x in itertools.combinations(df['address'].unique(), 2)]:

        c1 = df.loc[df['address'].isin([x[0]]), 'ID']
        c2 = df.loc[df['address'].isin([x[1]]), 'ID']
        c = pd.Series(list(set(c1).intersection(set(c2))))
        dfs.append(pd.DataFrame({'common users':len(c), 'pair of websites':' and '.join(x)}, index=[0]))
    return pd.concat(dfs)

common_users = df.groupby([df['used_at'].dt.year]).apply(f).reset_index(drop=True, level=1).reset_index()
print common_users

graph_by_common_users = common_users.pivot(index='pair of websites', columns='used_at', values='common users')
print graph_by_common_users

【问题讨论】:

  • 请说明您在旋转而不是 NaN 后想要什么?
  • 我有am.ru and cars.mail.ru/sale 2014cars.mail.ru/sale and am.ru 2015。当我尝试打印图形rects = ax.patches labels = [int(round(graph_by_common_users.loc[i, y])) for y in graph_by_common_users.columns.tolist() for i in graph_by_common_users.index] for rect, label in zip(rects, labels): height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2, height + 5, label, ha='center', va='bottom') 时出现错误,因为我有NaN。所以我想反转一些字符串,例如am.ru and cars.mail.ru/sale20142015
  • 我的解释清楚了吗?

标签: python pandas dataframe


【解决方案1】:

也许在旋转之前,尝试在" and " 上进行拆分,然后排序,使每一列的顺序相同:

df['pair of websites'] = df['pair of websites'].str.split(' and ')
df['pair of websites'] = df['pair of websites'].apply(lambda x: frozenset(sorted(x)))

似乎只要在每个条目的“和”部分中存在相同数量的空格,这应该可以工作。如果没有,您可能还必须使用str.strip()

【讨论】:

  • 它不会改变任何东西
  • @ldevyataykina 如果您在旋转之前这样做,它应该有。如果没有,我很想看看输出,以便我可以看到出了什么问题。
  • 第一个字符串返回TypeError: unhashable type: 'list'
  • @ldevyataykina 我的解决方案已编辑以解决该问题。请让我知道它现在是否有效。
  • 第一个字符串只在循环中起作用。但第二次返回AttributeError: 'list' object has no attribute 'apply'。我的代码是common_users = df.groupby([df['used_at'].dt.year]).apply(f).reset_index(drop=True, level=1).reset_index() for pairs in common_users['pair of websites']: pair = pairs.split(' and ') print pair.apply(lambda x: frozenset(sorted(x)))
【解决方案2】:

测试后我添加了倒置组合c_invert,因为在pivot 之后缺少一些值。现在所有combinationpivot 都很好用:

df = pd.read_csv("avito_trend.csv", 
                      parse_dates=[2])


def f(df):
    dfs = []
    for x in [list(x) for x in itertools.combinations(df['address'].unique(), 2)]:

        c1 = df.loc[df['address'].isin([x[0]]), 'ID']
        c2 = df.loc[df['address'].isin([x[1]]), 'ID']
        c = pd.Series(list(set(c1).intersection(set(c2))))
        #add inverted intersection c2 vs c1
        c_invert = pd.Series(list(set(c2).intersection(set(c1))))
        dfs.append(pd.DataFrame({'common users':len(c), 'pair of websites':' and '.join(x)}, index=[0]))
        #swap values in x
        x[1],x[0] = x[0],x[1]
        dfs.append(pd.DataFrame({'common users':len(c_invert), 'pair of websites':' and '.join(x)}, index=[0]))
    return pd.concat(dfs)

common_users = df.groupby([df['used_at'].dt.year]).apply(f).reset_index(drop=True, level=1).reset_index()
print common_users.pivot(index='pair of websites', columns='used_at', values='common users')
used_at                              2014  2015
pair of websites                               
am.ru and auto.ru                     408   224
am.ru and avito.ru                    579   262
am.ru and avtomarket.ru               133    72
am.ru and cars.mail.ru/sale           166    73
am.ru and drom.ru                     394   187
am.ru and e1.ru                       224    99
am.ru and irr.ru/cars                 223   102
auto.ru and am.ru                     408   224
auto.ru and avito.ru                 1602  1473
auto.ru and avtomarket.ru             243   162
auto.ru and cars.mail.ru/sale         330   195
auto.ru and drom.ru                   874   799
auto.ru and e1.ru                     475   451
auto.ru and irr.ru/cars               409   288
avito.ru and am.ru                    579   262
avito.ru and auto.ru                 1602  1473
avito.ru and avtomarket.ru            299   205
avito.ru and cars.mail.ru/sale        424   256
avito.ru and drom.ru                 1716  1491
avito.ru and e1.ru                   1364  1153
avito.ru and irr.ru/cars              602   403
avtomarket.ru and am.ru               133    72
avtomarket.ru and auto.ru             243   162
avtomarket.ru and avito.ru            299   205
avtomarket.ru and cars.mail.ru/sale   105    48
avtomarket.ru and drom.ru             247   175
avtomarket.ru and e1.ru               139   105
avtomarket.ru and irr.ru/cars         139    73
cars.mail.ru/sale and am.ru           166    73
cars.mail.ru/sale and auto.ru         330   195
cars.mail.ru/sale and avito.ru        424   256
cars.mail.ru/sale and avtomarket.ru   105    48
cars.mail.ru/sale and drom.ru         292   189
cars.mail.ru/sale and e1.ru           154   105
cars.mail.ru/sale and irr.ru/cars     197    94
drom.ru and am.ru                     394   187
drom.ru and auto.ru                   874   799
drom.ru and avito.ru                 1716  1491
drom.ru and avtomarket.ru             247   175
drom.ru and cars.mail.ru/sale         292   189
drom.ru and e1.ru                     634   539
drom.ru and irr.ru/cars               423   277
e1.ru and am.ru                       224    99
e1.ru and auto.ru                     475   451
e1.ru and avito.ru                   1364  1153
e1.ru and avtomarket.ru               139   105
e1.ru and cars.mail.ru/sale           154   105
e1.ru and drom.ru                     634   539
e1.ru and irr.ru/cars                 235   148
irr.ru/cars and am.ru                 223   102
irr.ru/cars and auto.ru               409   288
irr.ru/cars and avito.ru              602   403
irr.ru/cars and avtomarket.ru         139    73
irr.ru/cars and cars.mail.ru/sale     197    94
irr.ru/cars and drom.ru               423   277
irr.ru/cars and e1.ru                 235   148

如果你需要图表:

graph_by_common_users = common_users.pivot(index='pair of websites', columns='used_at', values='common users')
#sort by column 2014
graph_by_common_users = graph_by_common_users.sort_values(2014, ascending=False)



ax = graph_by_common_users.plot(kind='barh', width=0.5, figsize=(10,20))
[label.set_rotation(25) for label in ax.get_xticklabels()]


rects = ax.patches 
labels = [int(round(graph_by_common_users.loc[i, y])) for y in graph_by_common_users.columns.tolist() for i in graph_by_common_users.index] 
for rect, label in zip(rects, labels): 
    height = rect.get_height() 
    ax.text(rect.get_width() + 3, rect.get_y() + rect.get_height(), label, fontsize=8) 

【讨论】:

  • 很高兴能为您提供帮助。 :) 美好的一天。
  • 我想用平均值打印图表但它不起作用rects = ax.patches labels = [int(round(graph_by_common_users.loc[i, y])) for y in graph_by_common_users.columns.tolist() for i in graph_by_common_users.index] for rect, label in zip(rects, labels): height = rect.get_height() ax.text(rect.get_width() + 3, rect.get_y() + rect.get_height(), label, fontsize=8) 我做错了什么?
  • mean怎么算?
  • 我想打印列b 的平均值。像这样link
  • 所以我认为您可以添加新问题,因为我不知道您想如何计算mean?这个输入的期望输出是什么?因为mean 是来自一列(一个 Serie)的一个标量值。请解释清楚。
猜你喜欢
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 2018-01-07
  • 2018-10-09
  • 2021-01-06
相关资源
最近更新 更多