简体   繁体   中英

Concatenate strings in dataframe rows (Python - pandas)

Let's say I have the following dataframe d1:

d1 = pd.DataFrame(data = {'col1': ["A", "C"], 'col2': ["B", "D"]})

在此处输入图像描述

I want to built a dataframe d2 with a single row. That row would concatenate the values of d1's rows, separated by a space. That is:

d2 = pd.DataFrame(data = {'col1': ["A B"], 'col2': ["C D"]})

在此处输入图像描述

What should I do?

You can use pandas.apply with axis=1 for iterating over rows then rename columns like 0 -> col1 & 1 -> col2 .

d1 = pd.DataFrame(data = {'col1': ["A", "C"], 'col2': ["B", "D"]})
res = pd.DataFrame(d1.apply(lambda row: ' '.join(row), axis=1)).T.rename(columns = lambda x: f'col{x+1}')
print(res)

  col1 col2
0  A B  C D

You can also use agg to operate join . However, it will return a pd.Series , so convert it to a dataframe with pd.Series.to_frame :

d2 = d1.agg(' '.join).to_frame().T

Join the rows together with pd.apply with axis=1, convert the result to list and pass it to a new dataframe. The columns you can take from d1.

d2 = pd.DataFrame([d1.apply(" ".join, axis=1).tolist()], columns=d1.columns)
print(d2)
  col1 col2
0  A B  C D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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