簡體   English   中英

如果單元格沒有值,如何在特定行上添加值

[英]How to add value on a specific row if there is no value for the cell

如果行來自左側列並且單元格中沒有任何內容,或者如果行來自右側列並且單元格中沒有任何內容,我想向我的 dataframe 添加一些像“#Nothing#”這樣的值。 如果單元格中有東西,則應保持原樣。

練習是連接兩個數據框以便進行比較。 我使用了合並 function 並將指示符設置為 true,所以我知道哪些行來自 dataframe,我的問題是如何用我想要的值填充這些行中的空單元格。 如果兩者都保持原樣。

表格現在看起來像這樣:

some_name1_A some_name1_B some_name2_A some_name2_B _合並
left_only
right_only
兩個都

表格看起來應該是這樣的:

some_name1_A some_name1_B some_name2_A some_name2_B _合並
#沒有# #沒有# left_only
#沒有# #沒有# right_only
兩個都

我有一個想法,像這樣從主要的數據幀中制作幾個數據幀,然后將它們連接起來,但我收到了這個錯誤:

data = {'some_name1_A': ['One', None, 'One', None], 'some_name1_B': [None, 'Two', None, 'Two'], 'some_name2_A': ['Three', None, 'Three', None],
        'some_name2_B': [None, 'Four', None, 'Four'], '_merge': ['left_only', 'right_only', 'both', 'right_only']}
df = pd.DataFrame(data)

df_A = df.loc[df_A['_merge'] == 'left_only']
df_A.fillna('#Nothing#', inplace=True)
df_B = df.loc[df_B['_merge'] == 'right_only']
df_B.fillna('#Nothing#', inplace=True)

ValueError:無法在具有新類別的分類上設置項目,請先設置類別

both列中沒有的行上創建一個_merge fillna()

>>> # avoid duplicate typing and create the selection rule once
>>> indx = df._merge != 'both'

>>> # modify only those rows (where 'indx' is True)
>>> df.loc[indx] = df.loc[indx].fillna('bla')

結果:

>>> df
  some_name1_A some_name1_B some_name2_A some_name2_B      _merge
0          One          bla        Three          bla   left_only
1          bla          Two          bla         Four  right_only
2          One         None        Three         None        both
3          bla          Two          bla         Four  right_only

有條件地選擇值。 可以用循環替換以下內容。

df['some_name1_B'] = np.where((df['_merge'] == 'left_only') & df['some_name1_B'].isna(), '#Nothing#', df['some_name1_B'])
df['some_name2_B'] = np.where((df['_merge'] == 'left_only') & df['some_name2_B'].isna(), '#Nothing#', df['some_name2_B'])

df['some_name1_A'] = np.where((df['_merge'] == 'right_only') & df['some_name1_A'].isna(), '#Nothing#', df['some_name1_A'])
df['some_name2_A'] = np.where((df['_merge'] == 'right_only') & df['some_name2_A'].isna(), '#Nothing#', df['some_name2_A'])


some_name1_A    some_name1_B    some_name2_A    some_name2_B    _merge
0   One #Nothing#   Three   #Nothing#   left_only
1   #Nothing#   Two #Nothing#   Four    right_only
2   One None    Three   None    both
3   #Nothing#   Two #Nothing#   Four    right_only

可重現的例子:

df = pd.DataFrame({'A': ['One', np.nan, 'One'],
                   'B': ['Two', 'Two', np.nan]}, dtype='category')
print(df.dtypes)
print(df)

# Output:
A    category
B    category
dtype: object

     A    B
0  One  Two
1  NaN  Two
2  One  NaN

現在,如果您填寫缺失值:

>>> df.fillna('#Nothing#')
...
ValueError: Cannot setitem on a Categorical with a new category, set the categories first

如何解決這個問題? 在填充缺失值之前添加新類別:

cols = df.select_dtypes('category').columns
df[cols] = df[cols].apply(lambda x: x.cat.add_categories('#Nothing#'))
>>> df.fillna('#Nothing#')
           A          B
0        One        Two
1  #Nothing#        Two
2        One  #Nothing#

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM