1

I am trying to create a list that takes values from different files. I have three dataframes called for example "df1","df2","df3" each files contains two columns with data, so for example "df1" looks like this:

0, 1
1, 4
7, 7

I want to create a list that takes a value from first row in second column in each file, so it should look like this

F=[1,value from df2,value from df3]

my try

 import pandas as pd
 df1 = pd.read_csv(file1)
 df2 = pd.read_csv(file2)
 df3 = pd.read_csv(file3)


 F=[]
 for i in range(3):
   F.append(df{"i"}[1][0])

probably that is not how to iterate over, but I cannot figure out the correct way

scharette
  • 9,437
  • 8
  • 33
  • 67
serosse
  • 11
  • 1

1 Answers1

1

You can use iloc and list comprehension

vals = [df.iloc[0, 1] for df in [df1,df2,df3]]

iloc will get value from first row (index 0) and second column (index 1). If you wanted, say, value from third row and fourth column, you'd do .iloc[2, 3] and so forth.

As suggested by @jpp, you may use iat instead:

vals = [df.iat[0, 1] for df in [df1,df2,df3]]

For difference between them, check this and this question

rafaelc
  • 57,686
  • 15
  • 58
  • 82