这是我的数据框的样子:
account_type picture video
twitter NULL NULL
twitter https://pbs.twimg.com/media/EPlqKxKUEAARR_x.jpg NULL
twitter https://pbs.twimg.com/media/EPlqKxKUEAARR_x.jpg https://video.twimg.com/a
twitch NULL https://twitch.tv/
instagram https://scontent-lga3-1.cdninstagram.com NULL
instagram https://video-iad3-1.xx.fbcdn.net https://www.instagram.com/p
facebook https://graph.facebook.com/2 NULL
facebook NULL https://www.facebook.com/t
youtube https://i.ytimg.com/vi https://www.youtube.com/w
这就是我想要的外观:
account_type picture video post_type
twitter NULL NULL text
twitter https://pbs.twimg.com/media/EPlqKxKUEAARR_x.jpg NULL picture
twitter https://pbs.twimg.com/media/EPlqKxKUEAARR_x.jpg https://video.twimg.com/a video
twitch NULL https://twitch.tv/ video
instagram https://scontent-lga3-1.cdninstagram.com NULL picture
instagram https://video-iad3-1.xx.fbcdn.net https://www.instagram.com/p video
facebook https://graph.facebook.com/2 NULL picture
facebook NULL https://www.facebook.com/t video
youtube https://i.ytimg.com/vi https://www.youtube.com/w video
基本上,我试图将每一行分隔为图片/视频/文本。
For twitter, instagram
> if columns 'picture' and 'video are NULL,'post_type'= text
> if columns 'picture' is NOT NULL and 'video' is NULL, 'post_type'= picture
> if columns 'picture' is NOT NULL and 'video' is NOT NULL, 'post_type'= video
for twitch, youtube
> if 'video' is NOT NULL ,'post_type' = video
for facebook
> if 'video' is NULL ,'post_type' = picture
> if 'video' is NOT NULL ,'post_type' = video
我正在尝试根据null / notnull标准创建此文件。这是我尝试过的:
df['newtype'] = np.where(df['picture'].isnull(), '', 'picture')
df['newtype2'] = np.where(df['video'].isnull(), '', 'video')
但这会创建新的列。我希望所有条件都在指定条件的一列中。请告诉我是否有更好的方法可以做到这一点。
答案 0 :(得分:2)
您可以使用结构df.loc[condition, 'column']
并编写您的
# twitter-instagram
df.loc[(df['account_type'].isin(['twitter', 'instagram'])) &
df['video'].isnull() &
df['picture'].isnull(), 'post_type'] = 'text'
df.loc[(df['account_type'].isin(['twitter', 'instagram'])) &
df['video'].isnull() &
~df['picture'].isnull(), 'post_type'] = 'picture'
df.loc[(df['account_type'].isin(['twitter', 'instagram'])) &
~df['video'].isnull() &
~df['picture'].isnull(), 'post_type'] = 'video'
# twitch-youtube
df.loc[(df['account_type'].isin(['twitch', 'youtube'])) & ~df['video'].isnull(), 'post_type'] = 'video'
# facebook
df.loc[(df['account_type'] == 'facebook') & df['video'].isnull(), 'post_type'] = 'picture'
df.loc[(df['account_type'] == 'facebook') & ~df['video'].isnull(), 'post_type'] = 'video'