简体   繁体   中英

Need help in converting the for loop to list comprehension

need to import around 30 csv files to python , for that I have a for loop but I need to convert to list comprehension

The structure of all the files is same and they have same column names, out of all that I only need few of the columns as mentioned in the code and need to create one column which can differentiate the data

for Loop

import pandas as pd
cwd = os.getcwd()
dirs =os.listdir(cwd)

dfx=[]

for items in dirs:
    if items.find('GSVA')>0 and items.find('.csv')>0:
        x=cwd+'\\'+items
        temp_df=pd.read_csv((x),encoding = "ISO-8859-1")
        temp_df=temp_df.loc[::,['S.No.','Item','2014-15']]
        temp_df['State']=items.split('-')[1]
        dfx.append(temp_df)

gsva = pd.concat(dfx)

I have tried the below but not working

import os 
cwd = os.getcwd()
directory = os.listdir(cwd)

gsva = [(gsva.append(temp)
       temp['State']=items.split('-')[1]
       temp = temp.loc[::,['S.No.','Item','2014-15']]
       temp = pd.read_csv(cwd+'\\'+items)

       for items in directory
       if items.find('GSVA')>0 ]
gsva_1 = pd.concat(gsva)

import os
import pandas as pd

cwd = os.getcwd()

dirs = filter(lambda x: x[1]>=0 and x[2]>0, [(x, x.find('.csv'), x.find('GSVA')) for x in os.listdir(cwd)])
temp_df = [pd.read_csv((x),encoding = "ISO-8859-1") for x in [cwd+'\\'+x for x in dirs]]

def add_state(x):
    x['State'] = x.split('-')[1]
    return x

gsva = pd.concat(list(map(add_state, [temp_df.loc[::,['S.No.','Item','2014-15']] for x in temp_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