根据python pandas中的值选择行

2024-09-27 21:30:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个主题的文本文件,如下所示(称为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)]

Tags: texttxtpandasdf主题listfilepd
2条回答

您可能需要pip install xlrd才能使用.xlsx文件 否则,将数据保存到.csv并使用pd.read_csv()

另外,你发布的数据似乎有8列,但我认为只有4列,对吗? 如果没有,那么就需要解决重复变量名的问题。在

import pandas as pd

with open('subjects_visit2.txt', 'r') as infile:
    # put contents into a list without the newlines
    subject_list = infile.read().splitlines()

# convert subject_list to a list of integers
subject_list = [int(subject) for subject in subject_list]

# open data file and show 1st 5 rows
df = pd.read_excel('amygdala_mPFC_data_pandas.xlsx')
print(df.head())

# uses .query() which allows easy to read syntax.
# Note: The @ symbol allows access to objects not defined in the data frame
new_df = df.query('Subject in @subject_list')
print(new_df)

输出如下所示:

^{pr2}$

您可以使用以下方法:

In [5]: df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})

In [6]: df
Out[6]:
   A  B
0  5  1
1  6  2
2  3  3
3  4  5

In [7]: df[df['A'].isin([3, 6])]
Out[7]:
   A  B
1  6  2
2  3  3

顺便说一下,如果您使用笔记本电脑环境,最好使用:

^{pr2}$

编辑1:如果执行以下操作,会发生什么情况:

values_list = df_ROI1['Subject'].unique()

if "577" in values_list:
    print ("577 is in the dataframe and is a string")
elif 577 in values_list:
    print ("577 is in the dataframe and is an integer")
else:
    print ("577 is NOT in the dataframe")

编辑2:

所以你犯的错误是给算法一个字符串而不是一个整数。在

请尝试:

df_ROI1 = df_ROI1[df_ROI1['Subject'].isin([577])] # Without the quotes around 577
df_ROI1.head(n=5)

相关问题 更多 >

    热门问题