创建一个数据框并在熊猫中使用for循环填充该数据框

时间:2020-08-25 05:24:30

标签: python pandas dataframe for-loop

我在Python中有三个由字符向量组成的列表

 A=["A1", "B1", "C1"]
 B=["E1", "F1"]
 C=[]

我使用此方法创建了一个列表,如下所示

  import pandas as pd
  files_list=[]
  files_list.append(A)
  files_list.append(B) 
  files_list.append(C) 

我正在尝试创建以下数据框

   Value    Index
   A1       A
   B1       A
   C1       A
   D1       B
   E1       B
            C

我使用了以下循环

 L = pd.DataFrame()
 for file in files_list:
             page = file
             for j in page:
             #print (j)
             L["Value"]=j
             L["Index"]=file

我没有得到想要的输出,但是出现一个错误,提示值的长度(2)与索引的长度(3)不匹配。

我也尝试了以下代码

 L = pd.DataFrame()
    for file in files_list:
    page = file
    for j in page:
    data = list(zip((j), file))

这也不产生所需的输出,仅列出了很少的条目 我要求某人看看

1 个答案:

答案 0 :(得分:1)

我建议在此处创建字典,然后将值展平为传递给import pygame import math board_lines = [ ( 13,15,462,15 ), ( 13,469,462,469 ), #lin1 and line2,outer rect ( 62,86,409,86 ), ( 62,389,409,389 ), #line3 and l4,mid reect ( 114,186,360,186 ), ( 114,318,360,318 ), #line5,l6,internl rect ( 13,15,13,469 ), ( 462,12,462,469 ), #line9,l10,left and right sides ( 62,86,62,389 ), ( 409,85,409,389 ), #l7,l8left and right sides ( 114,186,114,316), ( 360,187,360,318 ), #l11,lin12left and right sides ( 237,15,237,186 ), ( 237,469,237,320 ), #upper V.line,lowerV ( 13,242,113,242 ), ( 360,242,462,242 ) #rIGHT LEFT hoRIZONTAL LINE ] pygame.init() intersectionPoints = [] for i, line1 in enumerate(board_lines): for line2 in board_lines[i:]: isectP = lineLineIntersect(line1[:2], line1[2:], line2[:2], line2[2:]) if isectP: intersectionPoints.append(isectP) # Define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) YELLOW_P = pygame.image.load('yellowgoti.png') BLUE_P = pygame.image.load('bluegoti.png') # This sets the WIDTH and HEIGHT of each grid location WIDTH = 20 HEIGHT = 20 # This sets the margin between each cell MARGIN = 5 # Create a 2 dimensional array. A two dimensional # array is simply a list of lists. grid = [] for row in range(19): # Add an empty array that will hold each cell # in this row grid.append([]) for column in range(19): grid[row].append(0) # Append a cell # Set row 1, cell 5 to one. (Remember rows and # column numbers start at zero.) grid[1][5] = 1 WINDOW_SIZE = [800, 600] screen = pygame.display.set_mode(WINDOW_SIZE) pygame.display.set_caption("Array Backed Grid") done = False clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Set the screen background screen.fill(BLACK) #screen.background(YELLOW_P,,600) # Draw the grid for row in range(19): for column in range(19): color = WHITE if grid[row][column] == 1: color = GREEN for line in board_lines: pygame.draw.line(screen, RED, line[:2], line[2:], 3) for isectP in intersectionPoints: pygame.draw.circle(screen, GREEN, isectP, 5) screen.blit(BLUE_P,(400,200)) # Limit to 60 frames per second clock.tick(60) pygame.display.flip() pygame.quit() 构造函数的元组列表:

DataFrame