简体   繁体   中英

How to select a fix range of rows of only 2 columns in a pandas dataframe from CSV file of 10 columns?

Say I have 1000 rows in my dataset and I need to append rows 100 to 200 to another dataframe.

import glob 
import pandas as pd
allFiles = glob.glob("*.csv")
dfs = []

for filename in allFiles:
    dfs.append(pd.read_csv(filename, usecols=["timeInterval_str", "Root"], loc[106:152] ))

 print(dfs)

I think you need instead:

loc[106:152]

use parameters skiprows and nrows in read_csv :

#first row is column name, so range 
skiprows=range(1,107), nrows=46

All together:

for filename in allFiles:
    df = pd.read_csv(filename, 
                     usecols=["timeInterval_str", "Root"], 
                     skiprows=range(1,107), 
                     nrows=46)
    dfs.append(df)

df = pd.concat(dfs, ignore_index=True)
print(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM