I want to extract only certain lines from multiple CSVs in pandas and make it CSV.

Asked 2 years ago, Updated 2 years ago, 39 views

It's been two weeks since I started using python.
Here's what I want to do:

p Read multiple CSVs using pandas and extract only the second line from each
②The line extracted from the first CSV is the first line
 The line extracted from the second CSV is the second line...
I'd like to do this, but
→All I can do is
·Read CSV in folder
→ Ambiguous or unknown
·I feel like I'm taking out the second line.
·I don't know how to combine them.
·I was able to write it out on CSV, but only one of them was extracted, and the procession was turned upside down.Why?

There are 120 CSVs in total
It contains numbers in 103 rows x 103 columns.
I'm afraid to try 120 pieces all of a sudden, so I'll start with 2 pieces.

Please tell me what to do.

This is the program

python pandas

2022-09-29 22:22

1 Answers

I think it works like this.

dfs=[]
for filename in filenames:
    df=pd.read_csv(filename, header=None)
    dfs.append(df.iloc[[1]])
merged_df=pd.concat(dfs, ignore_index=True)
merged_df.to_csv('out.csv')

I feel like I'm taking out the second line.

For the time being, the second line of data can be retrieved by df.iloc[1], but for df.iloc[1], the return value is Series type (one-dimensional data).This time, it would be more convenient to take it out as row data, so I think it would be better to use df.iloc[[1]].

I don't know how to combine.

Use pandas.concat() as the code for the question says in the comment out.This argument takes the form of calling all the DataFrames you want to combine into a list, so you should simply add them to the list in the loop and call them once outside the loop.


2022-09-29 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.