Python 如何使用列表中的数据帧值
我有一个dataframe,其中包含如下列中的值列表: 我想提取所有包含值“adhd”的行。我使用的代码:Python 如何使用列表中的数据帧值,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个dataframe,其中包含如下列中的值列表: 我想提取所有包含值“adhd”的行。我使用的代码: df[df["Label"] == "adhd"] 但它返回给我一个只有列名的空数据帧。是因为我的数据是列表格式的吗?是否有任何方法来处理此问题,以便我可以删除行、查看行并使用它们。我更愿意将它们保留为列表,因为许多行具有多个值,如[“adhd”、“抑郁”]。执行以下操作: df[df['Label'].apply(lambda x: 'adhd
df[df["Label"] == "adhd"]
但它返回给我一个只有列名的空数据帧。是因为我的数据是列表格式的吗?是否有任何方法来处理此问题,以便我可以删除行、查看行并使用它们。我更愿意将它们保留为列表,因为许多行具有多个值,如[“adhd”、“抑郁”]。执行以下操作:
df[df['Label'].apply(lambda x: 'adhd' in x)]
如果要将它们保留在列表中,可以执行如下所述的操作:
df[df['label'].apply(lambda x:x=['adhd'])#保留那些只有adhd的人
df[df['label'].apply(lambda x:adhd'in x)]#保留所有患有adhd的人
但是,我建议对您的数据进行一次热编码,即每个标签保留一列(例如,ADHD的一列,如果患者患有ADHD,则值为1,否则为0)
我认为最简单的方法是:
df['label'].应用(pd.系列)
这样您就可以更轻松地处理数据。无需应用方法的解决方案: 生成的示例数据帧:
example_df = pd.DataFrame(dict(data=[[str(i)] for i in range(0,10000,1000)],another_data=range(0,100000,10000)))
example_df
data another_data
0 [0] 0
1 [1000] 10000
2 [2000] 20000
3 [3000] 30000
4 [4000] 40000
5 [5000] 50000
6 [6000] 60000
7 [7000] 70000
8 [8000] 80000
9 [9000] 90000
example_df = example_df.explode('data')
>>>
data another_data
0 0 0
1 1000 10000
2 2000 20000
3 3000 30000
4 4000 40000
5 5000 50000
6 6000 60000
7 7000 70000
8 8000 80000
9 9000 90000
修复当前数据帧:
example_df = pd.DataFrame(dict(data=[[str(i)] for i in range(0,10000,1000)],another_data=range(0,100000,10000)))
example_df
data another_data
0 [0] 0
1 [1000] 10000
2 [2000] 20000
3 [3000] 30000
4 [4000] 40000
5 [5000] 50000
6 [6000] 60000
7 [7000] 70000
8 [8000] 80000
9 [9000] 90000
example_df = example_df.explode('data')
>>>
data another_data
0 0 0
1 1000 10000
2 2000 20000
3 3000 30000
4 4000 40000
5 5000 50000
6 6000 60000
7 7000 70000
8 8000 80000
9 9000 90000
得到筛选:
example_df[example_df['data'] == '1000']
>>>
data another_data
1 1000 10000
更新
要删除具有所需值的不需要的行,请执行以下操作:
example_df = example_df[example_df['data'] !='1000']
example_df
>>>
data another_data
0 0 0
2 2000 20000
3 3000 30000
4 4000 40000
5 5000 50000
6 6000 60000
7 7000 70000
8 8000 80000
9 9000 90000
试试这个
df=df[df[“Label”].astype(str).str.contains(“Label”)]
您好,我希望能帮助您。几周前我发现了这种方法
字典、列表、元组等。。。like对象(在较新版本的pandas中也可以称为stringdtype)
记住这一点,你可以做到这一点
example_df = [*([['foo']] * 79)]
example_df = pd.DataFrame({
'Label': [*example_df, ['Love']],
'Post': [*example_df, ['Another Value']]
})
example_df[
example_df.Label.str.contains('Love', regex = False)
]
这就是结果
您还可以使用数组中的位置(此方法为字符串数据类型带来了强大的功能)
请以文本而不是图像的形式发布数据帧作为示例。请尝试
df[df[“Label”]==[“adhd”]]
谢谢您,它成功了。但是如何删除包含“adhd”值的行?如果可以选择值,也可以删除它们:df=df[~df['label'].apply(lambda x:x=['adhd'])]