简体   繁体   English

在数据框python中合并不同行中的列

[英]Combine columns in different rows in dataframe python

I am trying to join columns in different rows in a dataframe. 我正在尝试联接数据框中不同行中的列。

import pandas as pd

tdf =  {'ph1': [1, 2], 'ph2': [3, 4], 'ph3': [5,6], 'ph4': [nan,nan]}

df = pd.DataFrame(data=tdf)

df

Output: 输出:

   ph1  ph2  ph3  ph4

0    1    3    5  nan

1    2    4    6  nan

I combined ph1, ph2, ph3, ph4 with below code: 我将ph1,ph2,ph3,ph4与以下代码合并:

for idx, row in df.iterrows():

        df = df[[ph1, ph2, ph3, ph4]]

        df["ConcatedPhoneNumbers"] = df.loc[0:].apply(lambda x: ', '.join(x), axis=1)

I got 我有

df["ConcatPhoneNumbers"]

ConcatPhoneNumbers

1,3,5,,

2,4,6,,

Now I need to combine these columns using pandas with appropriate function. 现在,我需要使用具有适当功能的熊猫来组合这些列。 My result should be 1,3,5,2,4,6 我的结果应该是1,3,5,2,4,6

Also need to remove these extra commas. 还需要删除这些多余的逗号。

I am new Python learner.I did some research and reached till here. 我是Python的新手,我做了一些研究,直到此为止。 Please help me to get the exact approach. 请帮助我获得确切的方法。

It seems you need stack for remove NaN s, then convert to int , str and list and last join : 看来您需要stack来删除NaN ,然后转换为intstrlist以及最后一个join

tdf =  {'ph1': [1, 2], 'ph2': [3, 4], 'ph3': [5,6], 'ph4': [np.nan,np.nan]}

df = pd.DataFrame(data=tdf)

cols = ['ph1', 'ph2', 'ph3', 'ph4']
s = ','.join(df[cols].stack().astype(int).astype(str).values.tolist())
print (s)
1,3,5,2,4,6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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