简体   繁体   English

如何将 dataframe 行合并为单行,每列的所有行值都集中在一起?

[英]How to merge dataframe rows to a single row with all row values concenated for each column?

I have a df like:我有一个像这样的df:

  | col1  | col2   | col3
0 | Text1 | a,b ,c | klra-tk³,t54 ? 
1 | Text2 | NaN    | gimbal3, gimbal4
2 | Text3 | a,k,m  | NaN

I want to get a single row with all unique values of a column in a single line and NaNs ignored like:我想在一行中获得一列的所有唯一值的单行,并且忽略 NaN,例如:

  | col1                | col2      | col3
0 | Text1, Text2, Text3 | a,b,c,k,m | klra-tk³,t54,gimbal3, gimbal4

How can I do this with pandas?我怎么能用 pandas 做到这一点?

Use custom function with Series.str.split , DataFrame.stack , reove duplicates by Series.drop_duplicates and remove missing values by Series.dropna , last join by , and convert Series to one row DataFrame by Series.to_frame and transpose: Use custom function with Series.str.split , DataFrame.stack , reove duplicates by Series.drop_duplicates and remove missing values by Series.dropna , last join by , and convert Series to one row DataFrame by Series.to_frame and transpose:

f = lambda x: ','.join(x.str.split(',', expand=True).stack().drop_duplicates().dropna())
df = df.apply(f).to_frame().T
print (df)
                col1       col2                         col3
0  Text1,Text2,Text3  a,b,c,k,m  klra-tk,t54,gimbal3,gimbal4

Or use list comprehension like:或使用列表理解,如:

f = lambda x: ','.join(x.str.split(',', expand=True).stack().drop_duplicates().dropna())
df = pd.DataFrame([[f(df[x]) for x in df.columns]], columns=df.columns)

暂无
暂无

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

相关问题 将一个 dataframe 的每一行乘以第二个 dataframe 中列的所有行 - Multiply each row of one dataframe by all rows of column in second dataframe 如何将 dataframe 中的每一行乘以不同 dataframe 的不同列,并将所有行的总和作为 Python 中的新列? - How to multiply each row in dataframe by a different column of different dataframe and get sum of all rows as a new column in Python? 尝试将多行中的列值混合/合并为一行 - Trying blend/merge column values across multiple rows into a single row Pandas dataframe 行按日期合并成一行 - Pandas dataframe rows merge by date in a single row 将 DataFrame 的单行中的所有元素除以同一行中另一列中的元素,然后使用 pandas 对所有行执行此操作? - Dividing all elements in a single row of a DataFrame by an element in another column in that same row and then do this for all rows using pandas? 如何在不改变列的情况下将数据框中所有行的值连接成一行? - How to concatenate values of all rows in a dataframe into a single row without altering the columns? 对于每一行,在列中找到所有具有相同值的行 - For each row, find all rows with same values in column 将 dataframe 中的多行与单行逐列进行比较 - Comparing multiple rows in dataframe to single row by column 如何计算行的列值与 dataframe 中具有多个值的所有其他行的差异? 迭代每一行 - How to calculate the difference of a row's column values against all other rows with multiple values in a dataframe? Iterate for every row 如何将每一行数据框合并到python中的列表中 - How to merge each row of dataframe into a list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM