Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

pandas - Stack multiple columns into one single column in a csv with Python 3.x

I have a large CSV file with multiple columns that I would like to merge into 2 columns using Python.

What I have:

ID.12345        ID.45678
CVE-xxxx-1234   CVE-xxxx-5678
CVE-xxxx-3456   

What I need:

ID         CVE
ID.12345   CVE-xxxx-1234
ID.12345   CVE-xxxx-3456
ID.45678   CVE-xxxx-5678

I looked through several solutions here but not sure where to start (coding n00b). This one looks closest to what I need but the data is already in a Pandas dataframe at the start while I only have the csv. Do I need Pandas? Do I need to create a dataframe from the csv file? Can this be done using only Python's csv library? HELP

P.S. The csv has 1000+ columns if that is of any significance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To get your expected result run:

result = df.melt(var_name='ID', value_name='CVE').dropna()

The result, for your data sample is:

         ID            CVE
0  ID.12345  CVE-xxxx-1234
1  ID.12345  CVE-xxxx-3456
2  ID.45678  CVE-xxxx-5678

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...