gpt4 book ai didi

python - Pandas : splitting a dataframe based on null values in a column

转载 作者:行者123 更新时间:2023-12-01 01:09:42 28 4
gpt4 key购买 nike

我有一个如下所示的数据框:

data = [['lynda', 10,'F',125,'5/21/2018'],['tom', np.nan,'M',135,'7/21/2018'], ['nick', 15,'F',99,'6/21/2018'], ['juli', 14,np.nan,120,'1/21/2018'],['juli', 19,np.nan,140,'10/21/2018'],['juli', 18,np.nan,170,'9/21/2018']]
df = pd.DataFrame(data, columns = ['Name', 'Age','Gender','Height','Date'])

df

Snapshot

如何根据性别的 np.NaN 值转换数据帧?

我希望将原始数据帧 df 拆分为 df1(Name,Age,Gender,Height,Date),其中包含性别值(df 的前 3 行)

并进入df2(Name,Age,Height,Date)其中没有性别列(df 的最后 3 行)

最佳答案

这是一种方法:

import pandas as pd
import numpy as np


data = [['lynda', 10,'F',125,'5/21/2018'],['tom', np.nan,'M',135,'7/21/2018'], ['nick', 15,'F',99,'6/21/2018'], ['juli', 14,np.nan,120,'1/21/2018'],['juli', 19,np.nan,140,'10/21/2018'],['juli', 18,np.nan,170,'9/21/2018']]
df = pd.DataFrame(data, columns = ['Name', 'Age','Gender','Height','Date'])

df2 = df[df['Gender'].notnull()].drop("Gender", axis=1)
print(df2)

输出:

    Name   Age  Height       Date
0 lynda 10.0 125 5/21/2018
1 tom NaN 135 7/21/2018
2 nick 15.0 99 6/21/2018

关于python - Pandas : splitting a dataframe based on null values in a column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54977459/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com