简体   繁体   English

固定由列表列表构成的数据框

[英]Fiix a data frame made by List of Lists

I'm new in this sorry, my problem is this: 抱歉,我是新手,我的问题是:

I made 24 table, from a .bed files then I made a list with all this tables, and I selected only a specific rows in every table, the result is another list, but when I try to convert in a dataframe only count every table as string. 我从一个.bed文件中制成了24个表,然后用所有这些表创建了一个列表,并且我只选择了每个表中的特定行,结果是另一个列表,但是当我尝试在数据帧中进行转换时,只计算每个表作为字符串。 every 24 table has the same numbers of columns 每24个表格的列数相同

I've tried with Pandas, called DataFrame and Series 我尝试过使用DataFrame和Series的Pandas

mylist = (chr1 , chr2, chr3, chr4, chr5, chr6, chr7, chr8, chr9, chr10, chr11, chr12, chr13, chr14, chr15, chr16, chr17, chr18, chr19, chr20, chr21, chr22, chrX, chrY)   

list2= []

for i in mylist:
   list2.append(i.loc[i[3]=='U28',:])

df = pd.DataFrame(list2)
df

This is my result of lis2, 这是我lis2的结果,

 [             0          1          2    3
  172       chr1      12061      12062  U28
  174       chr1      12064      12079  U28

 [176650 rows x 4 columns],              0          1          2    3
  9954      chr2      45229      45231  U28
  9978      chr2      45280      45284  U28
  9981      chr2      45288      45292  U28

 [132574 rows x 4 columns],              0          1          2    3
  1394      chr3      63185      63190  U28
  1396      chr3      63192      63197  U28
  1398      chr3      63206      63215  U28
  .....

and this happen when I called DataFrame 这发生在我打电话给DataFrame时

    0
0   0 1 2 3 172 ...
1   0 1 2 3 9954...
2   0 1 2 3 1394...
3   0 1 2 3 3516...
4   0 1 2 3 8894...

5 0 1 2 3 1471... 6 0 1 2 3 8385... ...... 5 0 1 2 3 1471 ... 6 0 1 2 3 8385 ... ......

and I'd like to merge all, in the same dataframe, like this 我想像这样合并所有数据

              0          1          2    3
 172       chr1      12061      12062  U28
 174       chr1      12064      12079  U28
 9954      chr2      45229      45231  U28
 9978      chr2      45280      45284  U28
 9981      chr2      45288      45292  U28
 1394      chr3      63185      63190  U28
 1396      chr3      63192      63197  U28
 1398      chr3      63206      63215  U28

In you example you need to iterate over each element of list2 and then append items to empty data frame. 在您的示例中,您需要遍历list2的每个元素,然后将项目附加到空数据框中。 See below example: 请参见以下示例:

=^..^= = ^ .. ^ =

import pandas as pd
from io import StringIO

# create raw data
raw_data = StringIO("""
0 1 2 3
172 chr1 12061 12062 U28
174 chr1 12064 12079 U28
""")

# load data into data frame
df = pd.read_csv(raw_data, sep=' ')


# create raw data
raw_data = StringIO("""
0 1 2 3
9954 chr2 45229 45231 U28
9978 chr2 45280 45284 U28
9981 chr2 45288 45292 U28
""")

# load data into data frame
df2 = pd.read_csv(raw_data, sep=' ')

# create list of data frames
df_list = [df, df2]

# append data frames
df_final = pd.DataFrame()
for item in df_list:
    df_final = df_final.append(item)

Output: 输出:

         0      1      2    3
172   chr1  12061  12062  U28
174   chr1  12064  12079  U28
9954  chr2  45229  45231  U28
9978  chr2  45280  45284  U28
9981  chr2  45288  45292  U28

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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