我有以下熊猫数据框:
df11 = pd.DataFrame(columns=["col1", "col2", "col3"])
df11.loc[1] = [2,["a","q","v"],["g","h","v"]]
df11.loc[2] = [21,["b","z","o"],["h"]]
df11.loc[3] = [11,["g","s","v"],["g","h","v"]]
df11.loc[4] = [2,["a","q","v"],["n","m","k","p"]]
如何用等效的字符串替换col2和col3中的数组?像这样:
df11 = pd.DataFrame(columns=["col1", "col2", "col3"])
df11.loc[1] = [2,"a,q,v","g,h,v"]
df11.loc[2] = [21,"b,z,o","h"]
df11.loc[3] = [11,"g,s,v","g,h,v"]
df11.loc[4] = [2,"a,q,v","n,m,k,p"]
答案 0 :(得分:3)
在循环中将str.join
与列名列表一起使用:
cols = ['col2','col3']
for c in cols:
df11[c] = df11[c].str.join(',')
或使用applymap
:
df11[cols] = df11[cols].applymap(','.join)
或列表理解解决方案:
L = [[','.join(y for y in x) for x in df11[c]] for c in cols]
df11[cols] = pd.DataFrame(L, columns=df11.index).T
print (df11)
col1 col2 col3
1 2 a,q,v g,h,v
2 21 b,z,o h
3 11 g,s,v g,h,v
4 2 a,q,v n,m,k,p