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
现在我需要使用具有适当功能的pandas来组合这些列。我的结果应该是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. Please help me to get the exact approach.
我是新的Python学习者。我做了一些研究,直到这里。请帮我准确一点。
1 个解决方案
#1
0
It seems you need stack
for remove NaN
s, then convert to int
, str
and list
and last join
:
看来你需要堆栈来删除NaNs,然后转换为int,str和list以及last 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
#1
0
It seems you need stack
for remove NaN
s, then convert to int
, str
and list
and last join
:
看来你需要堆栈来删除NaNs,然后转换为int,str和list以及last 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