我试图逐列迭代Python pandas创建的数据帧。虽然很容易让Python打印出整列,但我根本无法弄清楚如何将这列数据转换为列表或字符串,这样我就可以实际使用它包含的数据(在这种情况下,连接数据和复制它成为FASTA文件)。我的代码如下。任何建议将不胜感激。
import sys
import string
import shlex
import numpy as np
import pandas as pd
SNP_df = pd.read_csv('SNPs.txt',sep='\t',index_col = None ,header = None, nrows = 101)
output = open('100 SNPs.fa','a')
i=1
for i in SNP_df[i]:
data = SNP_df[i]
data = shlex.shlex(data, posix = True)
data.whitespace += "\n"
data.whitespace_split = True
data = list(data)
for j in data:
if j == 0:
output.write(("\n>%s\n")%(str(data(j))))
else:
output.write(data(j))
以下是我的数据文件的前几行:
位置REF AR_DM1005 AR_DM1015 AR_DM1050 AR_DM1056 AR_DM1088 AR_KB635 AR_KB652 AR_KB754 AR_KB819 AR_KB820 AR_KB827 AR_KB945 AR_MSH126 AR_MSH51 PP_BdA1134-13 PP_BdA1137-10 PP_DM1038 PP_DM1049 PP_DM1054 PP_DM1065 PP_DM1081 PP_DM1084 PP_JR83 ST_JR138 ST_JR158 ST_JR209 ST_JR72 ST_JR84 ST_JR91 ST_MSH177 ST_MSH217 CH_JR198 CH_JR20 CH_JR272 CH_JR356 CH_JR377 CH_KB888 CH_MSH202 TL_MA1959 TL_MSH130 TL_SCI12-2 TL_SPE123_2 -3 TL_SPE123_5-1 TL_SPE123_6-3 TL_SPE123_7-1 TL_SPE123_8-1 CU_SPE123_1-2 CU_SPE123_4-1 Dmir_SP138
55 C T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T
380 G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G
391 A A G A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
402 G A A A A G A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A G A A A A A A A A A A A
422 A C C C C C C C C C C C C A C C C C C C C C C C C C C C C C C C C C C C C C C C C A
564 G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G
答案 0 :(得分:0)
只是使用numpy!您可以轻松地将Series(1列DataFrame)转换为1D numpy数组!
import numpy as np
for i in SNP_df:
data = SNP_df[i]
data = np.array(data)
for j in data:
if j == 0:
output.write(("\n>%s\n")%(str(data(j))))
else:
output.write(data(j))
答案 1 :(得分:0)
使用您的示例数据。请注意,由于复制和粘贴选项卡,因为白色空格(所以使用sep ='\ s +',iso'\ t'),我已将数据的第一行设置为列名(不使用header = None)。可以使用join来完成将一列连接到字符串。
In [20]: from StringIO import StringIO
In [21]: data = """\
....: POSITION REF AR_DM1005 AR_DM1015 AR_DM1050 AR_DM1056 AR_DM1088 AR_KB635 AR_KB652 AR_KB754 AR_KB819 AR_KB820 AR_KB827 AR_KB945 AR_MSH126 AR_MSH51 PP_BdA1134-13 PP_BdA1137-10 PP_DM1038 PP_DM1049 PP_DM1054 PP_DM1065 PP_DM1081 PP_DM1084 PP_JR83 ST_JR138 ST_JR158 ST_JR209 ST_JR72 ST_JR84 ST_JR91 ST_MSH177 ST_MSH217 CH_JR198 CH_JR20 CH_JR272 CH_JR356 CH_JR377 CH_KB888 CH_MSH202 TL_MA1959 TL_MSH130 TL_SCI12-2 TL_SPE123_2-3 TL_SPE123_5-1 TL_SPE123_6-3 TL_SPE123_7-1 TL_SPE123_8-1 CU_SPE123_1-2 CU_SPE123_4-1 Dmir_SP138
....: 55 C T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T C
....: 380 G G A A G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G A G G G G G G G G G
....: 391 A A G A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
....: 402 G A A A A G A A A A A A A A A G A A A A A A A A A A A A A A A A A A A G A A A G A A A A G A A A A G
....: 422 A C C C C C C C C C C C C C C A A C C C C C C C C C C C C C C C C C C A C C C A C C C C A C C C C A
....: 564 G G G G G G G G G G G G G G G G G G G G G G G G A A G G G G G G A G G G G G G G G G G G G G G G G G
....: """
In [22]: import pandas as pd
In [23]: SNP_df = pd.read_csv(StringIO(data), sep='\s+', index_col=None, nrows=101)
In [24]: SNP_df['AR_DM1005']
Out[24]:
0 T
1 G
2 A
3 A
4 C
5 G
Name: AR_DM1005, dtype: object
In [25]: ''.join(SNP_df['AR_DM1005'])
Out[25]: 'TGAACG'