数据错误使用函数和 groupby 合并 pandas 数据框中的字符串
Data Error Using function and groupby to union strings in pandas dataframe
我有以下结构的数据框:
mydf:
Entry Address ShortOrdDesc
0 988 Fake Address 1 SC_M_W_3_1
1 989 Fake Address 2 SC_M_W_3_3
2 992 Fake Address 3 nan_2
3 992 SC_M_G_1_1
4 992 SC_M_O_1_1
在此 df 上还有一些工作要做,以合并具有相同 Entry 的行。对于这些只有第一行有 地址 。我需要连接 ShortOrdDesc 列和 Address。我发现了一个非常有用的link:
Pandas groupby: How to get a union of strings
以此为基础,我开发了以下功能:
def f(x):
return pd.Series(dict(A = x['Entry'].sum(),
B = x['Address'].sum(),
C = "%s" % '; '.join(x['ShortOrdDesc'])))
使用
应用
myobj = ordersToprint.groupby('Entry').apply(f)
这个returns错误:
TypeError: must be str, not int
查看我的数据,我看不出问题是什么,因为 运行 .sum() 对 'Entry' 的整数应该有效我相信。
我的代码或方法有什么错误?
我认为某些列是数字,需要 string
。
所以使用astype
and if need remove NaN
s add dropna
:
def f(x):
return pd.Series(dict(A = x['Entry'].sum(),
B = ''.join(x['Address'].dropna().astype(str)),
C = '; '.join(x['ShortOrdDesc'].astype(str))))
myobj = ordersToprint.groupby('Entry').apply(f)
print (myobj)
A B C
Entry
988 988 Fake Address 1 SC_M_W_3_1
989 989 Fake Address 2 SC_M_W_3_3
992 2976 Fake Address 3 nan_2; SC_M_G_1_1; SC_M_O_1_1
另一种解决方案 agg
,但需要重命名列:
f = {'Entry':'sum',
'Address' : lambda x: ''.join(x.dropna().astype(str)),
'ShortOrdDesc' : lambda x: '; '.join(x.astype(str))}
cols = {'Entry':'A','Address':'B','ShortOrdDesc':'C'}
myobj = ordersToprint.groupby('Entry').agg(f).rename(columns=cols)[['A','B','C']]
print (myobj)
A B C
Entry
988 988 Fake Address 1 SC_M_W_3_1
989 989 Fake Address 2 SC_M_W_3_3
992 2976 Fake Address 3 nan_2; SC_M_G_1_1; SC_M_O_1_1
我有以下结构的数据框:
mydf:
Entry Address ShortOrdDesc
0 988 Fake Address 1 SC_M_W_3_1
1 989 Fake Address 2 SC_M_W_3_3
2 992 Fake Address 3 nan_2
3 992 SC_M_G_1_1
4 992 SC_M_O_1_1
在此 df 上还有一些工作要做,以合并具有相同 Entry 的行。对于这些只有第一行有 地址 。我需要连接 ShortOrdDesc 列和 Address。我发现了一个非常有用的link:
Pandas groupby: How to get a union of strings
以此为基础,我开发了以下功能:
def f(x):
return pd.Series(dict(A = x['Entry'].sum(),
B = x['Address'].sum(),
C = "%s" % '; '.join(x['ShortOrdDesc'])))
使用
应用myobj = ordersToprint.groupby('Entry').apply(f)
这个returns错误:
TypeError: must be str, not int
查看我的数据,我看不出问题是什么,因为 运行 .sum() 对 'Entry' 的整数应该有效我相信。
我的代码或方法有什么错误?
我认为某些列是数字,需要 string
。
所以使用astype
and if need remove NaN
s add dropna
:
def f(x):
return pd.Series(dict(A = x['Entry'].sum(),
B = ''.join(x['Address'].dropna().astype(str)),
C = '; '.join(x['ShortOrdDesc'].astype(str))))
myobj = ordersToprint.groupby('Entry').apply(f)
print (myobj)
A B C
Entry
988 988 Fake Address 1 SC_M_W_3_1
989 989 Fake Address 2 SC_M_W_3_3
992 2976 Fake Address 3 nan_2; SC_M_G_1_1; SC_M_O_1_1
另一种解决方案 agg
,但需要重命名列:
f = {'Entry':'sum',
'Address' : lambda x: ''.join(x.dropna().astype(str)),
'ShortOrdDesc' : lambda x: '; '.join(x.astype(str))}
cols = {'Entry':'A','Address':'B','ShortOrdDesc':'C'}
myobj = ordersToprint.groupby('Entry').agg(f).rename(columns=cols)[['A','B','C']]
print (myobj)
A B C
Entry
988 988 Fake Address 1 SC_M_W_3_1
989 989 Fake Address 2 SC_M_W_3_3
992 2976 Fake Address 3 nan_2; SC_M_G_1_1; SC_M_O_1_1