![](/img/trans.png)
[英]Pandas: iteratively concatenate columns stored in a dictionary of dataframes
[英]Iteratively concatenate columns in pandas with NaN values
我有一個pandas.DataFrame
數據框:
import pandas as pd
df = pd.DataFrame({"x": ["hello there you can go home now", "why should she care", "please sort me appropriately"],
"y": [np.nan, "finally we were able to go home", "but what about meeeeeeeeeee"],
"z": ["", "alright we are going home now", "ok fine shut up already"]})
cols = ["x", "y", "z"]
我想迭代地連接這些列,而不是像這樣寫:
df["concat"] = df["x"].str.cat(df["y"], sep = " ").str.cat(df["z"], sep = " ")
我知道將三列匯總起來似乎很瑣碎,但實際上我有30列。因此,我想做些類似的事情:
df["concat"] = df[cols[0]]
for i in range(1, len(cols)):
df["concat"] = df["concat"].str.cat(df[cols[i]], sep = " ")
現在,初始df["concat"] = df[cols[0]]
行可以正常工作,但是位置df.loc[1, "y"]
的NaN
值使連接混亂。 最終,由於這一空值,整個1
行在df["concat"]
以NaN
結尾。 我該如何解決? 我需要指定pd.Series.str.cat
有某些選項嗎?
選項1
pd.Series(df.fillna('').values.tolist()).str.join(' ')
0 hello there you can go home now
1 why should she care finally we were able to go...
2 please sort me appropriately but what about me...
dtype: object
選項2
df.fillna('').add(' ').sum(1).str.strip()
0 hello there you can go home now
1 why should she care finally we were able to go...
2 please sort me appropriately but what about me...
dtype: object
選項3
In [3061]: df.apply(lambda x: x.str.cat(sep=''), axis=1)
Out[3061]:
0 hello there you can go home now
1 why should she carefinally we were able to go ...
2 please sort me appropriatelybut what about mee...
dtype: object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.