繁体   English   中英

Python:在DataFrame中,如何查找某一列中的字符串出现在另一列中的年份?

[英]Python: In a DataFrame, how do I find the year that strings from one column appear in another column?

我有一个数据框,想要遍历c2列中的所有字符串,并打印该字符串及其出现在c2列中的年份,然后还打印出出现在c1列中的第一年 (如果它存在于c1中)。 然后在另一列中计算年份之间的差异。 c2中有NaN值。

df示例:

id   year     c1                c2
0    1999     luke skywalker    han solo
1    2000     leia organa       r2d2
2    2001     han solo          finn
3    2002     r2d2              NaN
4    2004     finn              c3po
5    2002     finn              NaN
6    2005     c3po              NaN

示例打印结果:

c2            year in c2   year in c1     delta
han solo      1999         2001           2
r2d2          2000         2002           2
finn          2001         2004           3
c3po          2004         2005           1

我正在将Jupyter Notebook与python和pandas一起使用。 谢谢!

您可以按照以下步骤进行操作:

df1 = df[df.c2.notnull()].copy()

s = df.groupby('c1')['year'].first()
df1['year in c1'] = df1.c2.map(s)

df1 = df1.rename(columns={'year':'year in c2'})

df1['delta'] = df1['year in c1'] - df1['year in c2']

print(df1[['c2','year in c2','year in c1', 'delta']])

输出:

         c2  year in c2  year in c1  delta
0  han solo        1999        2001      2
1      r2d2        2000        2002      2
2      finn        2001        2004      3
4      c3po        2004        2005      1

这是一种方法。

df['year_c1'] = df['c2'].map(df.groupby('c1')['year'].agg('first'))\
                        .fillna(0).astype(int)

df = df.rename(columns={'year': 'year_c2'})
df['delta'] = df['year_c1'] - df['year_c2']

df = df.loc[df['c2'].notnull(), ['id', 'year_c2', 'year_c1', 'delta']]

#    id  year_c2  year_c1  delta
# 0   0     1999   2001.0      2
# 1   1     2000   2002.0      2
# 2   2     2001   2004.0      3
# 4   4     2004   2005.0      1

说明

  • c1映射到year ,按“ first”进行聚合。
  • 使用此映射在c2上计算year_c1
  • 计算deltayear_c2year_c1之间的差。
  • 删除c2和order列中具有null行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM