Python 基于另一列的空值在pandas中获取新列
这就是我的数据帧的外观:Python 基于另一列的空值在pandas中获取新列,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,这就是我的数据帧的外观: account_type picture video twitter NULL NULL twitter https://pbs.twimg.com/media/EPlqKxKUEAARR_x.jpg NULL twitter https://pbs.twimg
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')
但这会创建新的列。我希望所有内容都在一列中,并带有指定的条件。请告诉我是否有更好的方法。您可以使用构造
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'
您可以使用构造
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'
非常感谢你。这很有魅力。我不知道我们可以像那样使用df.loc。有什么可以提高我熊猫技能的技巧/来源吗?也许你也可以看看
np。选择查看多个条件和多个结果:)我没有测试它,但看起来它会适合用例谢谢。这很有魅力。我不知道我们可以像那样使用df.loc。有什么我可以提高熊猫技能的技巧/来源吗?也许你也可以看看np。选择获得多个条件和多个结果:)我没有测试它,但看起来它适合用例