Python Dataframe中的子集/筛选器列
我有这样的数据帧:Python Dataframe中的子集/筛选器列,python,pandas,dataframe,subset,Python,Pandas,Dataframe,Subset,我有这样的数据帧: text emotion 0 working add oil [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0] 1 you're welcome [0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0] 7 off to face my exam now [0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, ... 12 n
text emotion
0 working add oil [1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0]
1 you're welcome [0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0]
7 off to face my exam now [0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, ...
12 no, i'm so not la! i want to sleeeeeeeeeeep. [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, ...
151 i try to register on ebay. when i enter my hom... [1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, ...
18 Swam 6050 yards on just a yogurt for breakfast... [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, ...
19 Alright! [0, 0, 1, 1, 0, 0, 0, 0]
120 Visiting gma. It's getting cold [0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, ...
22 You are very missed [0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, ...
345 ...LOL! You mean Rhode Island...close enough [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, ...
我如何才能只保留情感栏中的第一个数字,以获得这样的数据
text emotion
0 working add oil 1
1 you're welcome 0
7 off to face my exam now 0
12 no, i'm so not la! i want to sleeeeeeeeeeep. 0
151 i try to register on ebay. when i enter my hom... 1
18 Swam 6050 yards on just a yogurt for breakfast... 0
19 Alright! **0**
120 Visiting gma. It's getting cold 0
22 You are very missed **0**
345 ...LOL! You mean Rhode Island...close enough 0
如果“情感”列是列表而不是字符串:
df["emotion"] = df["emotion"].apply(lambda x: x[0])
print(df)
印刷品:
文本情感
0工作时添加机油1
1不客气0
2现在开始面对我的考试0
不,我不是洛杉矶人!我想去溜溜。0
我尝试在易趣上注册。当我进入我的领域。。。1.
早餐吃酸奶游6050码。。。0
6好吧!0
7访问gma。天气变冷了
非常想念你
9…哈哈!你是说罗德岛…离得够近了吗
如果是字符串,可以使用
ast.literal\u eval
将其转换为列表:
from ast import literal_eval
df["emotion"] = df["emotion"].apply(literal_eval)
# and then:
df["emotion"] = df["emotion"].apply(lambda x: x[0])