我有一个主题的文本文件,如下所示(称为subjects_visit1.txt):
577
610
650
770
883
以及一张如下所示的Excel表格:
^{pr2}$我想用我加载的文本文件中的主题数据制作一个新的df,但是我下面的代码一直没有工作。我的主题列表的数据类型有问题吗?还是别的什么?在
import pandas as pd
# load text file of subject #s
subject_list = open('subjects_visit2.txt', 'r')
lines = subject_list.read().split('\n')
subjs = list(lines)
newfile = pd.ExcelFile('amygdala_mPFC_data_pandas.xlsx')
df_ROI1 = newfile.parse("01")
# restrict to subject #s in text file
print df_ROI1['Subject'].isin(subjs)
df_ROI1 = df_ROI1[df_ROI1['Subject'].isin(subjs)]
您可能需要
pip install xlrd
才能使用.xlsx文件 否则,将数据保存到.csv并使用pd.read_csv()
另外,你发布的数据似乎有8列,但我认为只有4列,对吗? 如果没有,那么就需要解决重复变量名的问题。在
输出如下所示:
^{pr2}$您可以使用以下方法:
顺便说一下,如果您使用笔记本电脑环境,最好使用:
^{pr2}$编辑1:如果执行以下操作,会发生什么情况:
编辑2:
所以你犯的错误是给算法一个字符串而不是一个整数。在
请尝试:
相关问题 更多 >
编程相关推荐