pandas 用字符串制作列

8zzbczxx  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(104)

试图使列的值为1和2(真,假)出不好的列!发现了一个手动劳动的方式检查和插补值一个接一个,并检查他们通过包含模块,但我想循环超过他们!
我的数据

`df['activities']
`
0                                                                    Game | jogging  | self teaching
1                                        Game | outside food  | junk food  | with GF | self teaching
2                                                    Game | outside food  | jogging  | self teaching
3                                                      outside food  | fab | with GF | self teaching
4       eating in restaurant | drinking  | Game | outside food  | with GF | jogging  | self teaching

字符串
唯一的好办法就是大量的劳动!

`df["Working out"]=df["activities"].str.contains("jogging |indoor workout |gym ")


“我打扫了一下,分开了,试着把错误的答案翻过来;

df["activities"]=df["activities"].str.replace("|","",regex=True)
df["activities"]=df["activities"].str.split("  ")
activities=[]
for act in df["activities"]:
    if act not in activities:
        activities.append(act)

cigdeys3

cigdeys31#

import pandas as pd

df = pd.DataFrame({
    'activities': [
        'Game | jogging | self teaching',
        'Game | outside food | junk food | with GF | self teaching',
        'Game | outside food | jogging | self teaching',
        'outside food | fab | with GF | self teaching',
        'eating in restaurant | drinking | Game | outside food | with GF | jogging | self teaching'
    ]
})

df['activities'] = df['activities'].str.lower().str.strip()

df_activities = df['activities'].str.split(' \| ', expand=True).stack().str.strip().reset_index()

unique_activities = df_activities[0].unique()

for activity in unique_activities:
    df[activity] = df['activities'].str.contains(activity).map({True: 1, False: 2})

print(df)

字符串


的数据

相关问题