实例如下所示:>>>importpandasaspd>>>df=pd.DataFrame({"x":['a','b','c','d'],"y":['aa','bb','cc',
实例如下所示:
>>> 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进行列相加,行相加的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
dataframe 相加