Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 代码在字符串拆分中工作不正常_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 代码在字符串拆分中工作不正常

Python 代码在字符串拆分中工作不正常,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有这样的数据帧 df['category'] 0 None 1 None 2 None 3 None Name: category, dtype: object 我想应用下面的代码并将它们分成两列,分别为level1和level2 代码 if df['category'] is None: riskc = pd.DataFrame(columns=['Level1', 'Level2']) else: riskc=df['category'].st

我有这样的数据帧

 df['category'] 
0    None
1    None
2    None
3    None
Name: category, dtype: object
我想应用下面的代码并将它们分成两列,分别为
level1
level2

代码

if df['category'] is None:
    riskc = pd.DataFrame(columns=['Level1', 'Level2'])
else:
    riskc=df['category'].str.split(',', expand=True).melt()['value']\
          .str.split(':', expand=True).rename(columns={0:'Level1', 1:'Level2'})
电流输出

riskc

    Level1
0   None
1   None
2   None
3   None
预期产出

riskc

    Level1
0   None
1   None
2   None
3   None
风险C


我的代码中有什么错误要检查df['category']是否包含所有非,您需要使用:

if df['category'].isna().all():
    riskc = pd.DataFrame(columns=['Level1', 'Level2'])

Works,我犯了什么错误?你将df['category']与None进行比较,但df['category'](即使其所有元素都为None)是一个系列,而不是None。