如何使用dataframe修复此特定示例中的TypeError?

2024-04-25 22:39:16 发布

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

不久前,我编写了一个脚本来过滤数据帧(DF)中超过特定值的元素。 现在,我重用这个脚本来过滤CSV文件中DF中的其他值。 以下是文件的一部分,您可以看到它的外观:

Partition,Site,ES,EN,S,N,P[S],dS,dN,dN-dS,P [dN/dS > 1],P [dN/dS < 1],Total branch length
1,1,0,3.000000000000001,0,0,0,null,0,null,1,1,39.076790211622
1,2,0.5566572827464659,2.283438288771876,2.75,18.25,0.1959994897104366,4.940203039169632,7.992333355247175,0.07810596263277007,0.3385048004154001,0.8414604110057038,39.076790211622
...

以下是数据“解析”(只是为了更好地查找帖子):

Partition   Site    ES  EN  S   N   P[S]    dS  dN  dN-dS   P [dN/dS > 1]   P [dN/dS < 1]   Total branch length

1   1   0   3.000000000000001   0   0   0   null    0   null    1   1   39.076790211622

1   2   0.5566572827464659  2.283438288771876   2.75    18.25   0.1959994897104366  4.940203039169632   7.992333355247175   0.07810596263277007 0.3385048004154001  0.8414604110057038  39.076790211622

以下是筛选数据的脚本:

import pandas as pd

# Create DF with specific column names
df_S_SLAC = pd.read_csv("S_SLAC.csv",sep=",", names=['dN-dS',"P [dN/dS > 1]","P [dN/dS < 1]"])

# Filter all values <= value of interest and write a new file with the filtered values 
S_greather_than = (df_S_SLAC["dN-dS"] < 5) & (df_S_SLAC["P [dN/dS > 1]"] < 5) & (df_S_SLAC["P [dN/dS < 1]"] < 5)
df_S_SLAC.loc[S_greather_than]
df_S_SLAC.loc[S_greather_than].to_csv(".../S_SLAC_v1.csv")

所以,这个脚本给了我这个错误:

TypeError: ‘<' not supported between instances of 'str' and 'int'

我理解这个错误的部分原因是,在尝试过滤数据时,我找不到错误的原因。 那么,有可能解决这个问题,或者我可以编辑输入文件或/和脚本

欢迎任何建议