>>> import pandas as pd
>>> df = pd.DataFrame({"x":['a','b','c','d'],"y":['aa','bb','cc','dd'],"z":['aaa','bbb','ccc','ddd']})
>>> df
x y z
0 a aa aaa
1 b bb bbb
2 c cc ccc
3 d dd ddd
【1】对列的字符串进行相连
>>> "_".join(df['x'])
'a_b_c_d'
>>> "".join(df['x'])
'abcd'
【2】对行的字符串进行相连,结果存在新的一列
>>> df['a'] = df['x']+df['y']+df['z']
>>> df
x y z a
0 a aa aaa aaaaaa
1 b bb bbb bbbbbb
2 c cc ccc cccccc
3 d dd ddd dddddd
>>>
对dataframe进行列相加,行相加
最新推荐文章于 2024-09-19 07:09:30 发布