简体   繁体   English

如何使用 pandas 将多行转换为同一 ID 的单行

[英]how to convert multiple rows into single row for same id using pandas

I have text file, in below format and it has unique IDs and each unique IDs have four rows, now I need to convert into single row for particular ID.我有以下格式的文本文件,它具有唯一 ID,每个唯一 ID 有四行,现在我需要将特定 ID 转换为单行。 let say if have 8 rows and the output should give 2 rows.假设如果有 8 行,output 应该有 2 行。 And it doesn't have header which I need do using pandas!而且它没有 header ,我需要使用熊猫!

    xyz,name,,,12345
    2nd street,add,,,12345
    xyx@mail.com,email,,,12345
    575xxx5678,contact,,,12345

output output

xyz,name,,,12345,2nd street,add,,,12345,xyx@mail.com,email,,,12345,575xxx5678,contact,,,12345

Consider unique ID as 12345, can help me to resolve this.将唯一 ID 视为 12345,可以帮助我解决此问题。 It would be great.那会很好。 Thanks in Advance.提前致谢。

Suppose you have this file.csv :假设你有这个file.csv

www,contact,,,99999
xyz,name,,,12345
2nd street,add,,,12345
xyx@mail.com,email,,,12345
575xxx5678,contact,,,12345
qqq,contact,,,99999

To read it to pandas:要将其读取到 pandas:

df = pd.read_csv("file.csv", names=["col1", "col2", "col3", "col4", "ID"])
print(df)

Prints:印刷:

           col1     col2  col3  col4     ID
0           www  contact   NaN   NaN  99999
1           xyz     name   NaN   NaN  12345
2    2nd street      add   NaN   NaN  12345
3  xyx@mail.com    email   NaN   NaN  12345
4    575xxx5678  contact   NaN   NaN  12345
5           qqq  contact   NaN   NaN  99999

Then to convert it to your desired output:然后将其转换为您想要的 output:

x = (
    df.assign(ID2=df["ID"])
    .groupby("ID")
    .agg(list)
    .apply(lambda x: [v for l in zip(*x) for v in l], axis=1)
)

pd.DataFrame(x.tolist()).to_csv("output.txt", sep=",", header=None, index=None)

This creates output.txt :这将创建output.txt

xyz,name,,,12345,2nd street,add,,,12345,xyx@mail.com,email,,,12345.0,575xxx5678,contact,,,12345.0
www,contact,,,99999,qqq,contact,,,99999,,,,,,,,,,

暂无
暂无

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

相关问题 如何在 pandas 中使用 id 将多行合并为一行多列(将具有相同 id 的多条记录聚集到一条记录中) - How to combine multiple rows into a single row with many columns in pandas using an id (clustering multiple records with same id into one record) 使用 Pandas 将多行文本转换为单行 - Convert multiple rows of text into a Single row using Pandas 如何使用多索引将 pandas dataframe 中的单行与多行相加? - How to sum single row to multiple rows in pandas dataframe using multiindex? 如何使用pandas在同一行索引下有多行 - How to have multiple rows under the same row index using pandas 如何将具有相同 ID 但两列中的不同值的行分组为一行,将不同的值作为 Pandas 中的列? - How to group rows with same ID but different values in two columns into a single row the different values as columns in Pandas? Python Pandas:将多行转换为单行,而忽略NaN - Python Pandas : Convert multiple rows into single row, ignoring NaN's pandas 中的多行变成单行 - multiple rows into single row in pandas 如何使用行字符串的子集在 pandas dataframe 分组中将多行组合成单行 - How to combine multiple rows into a single row in pandas dataframe grouping using a subset of the row string 如何在 pandas 中将行转换为单行? - how do I convert a n rows to a single row in pandas? 使用相同的Id,pandas从多个列表创建多个行 - creating multiple rows from multiple list using same Id, pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM