【问题标题】:drop.na() not working on dataframe with Nan values?drop.na() 不适用于具有 Nan 值的数据帧?
【发布时间】:2022-10-01 00:10:04
【问题描述】:

我有一个带有 Nan 值的数据框。出于某种原因,当我尝试删除这些行时,df.dropna() 不起作用。有什么想法吗?

行示例:


30754   22  Nan Nan Nan Nan Nan Nan Jewellery-Women N

df = pd.read_csv(\'/Users/xxx/Desktop/CS 677/Homework_4/FashionDataset.csv\')

df.dropna()

df.head().to_dict()


{\'Unnamed: 0\': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4},
 \'BrandName\': {0: \'life\',
  1: \'only\',
  2: \'fratini\',
  3: \'zink london\',
  4: \'life\'},
 \'Deatils\': {0: \'solid cotton blend collar neck womens a-line dress - indigo\',
  1: \'polyester peter pan collar womens blouson dress - yellow\',
  2: \'solid polyester blend wide neck womens regular top - off white\',
  3: \'stripes polyester sweetheart neck womens dress - black\',
  4: \'regular fit regular length denim womens jeans - stone\'},
 \'Sizes\': {0: \'Size:Large,Medium,Small,X-Large,X-Small\',
  1: \'Size:34,36,38,40\',
  2: \'Size:Large,X-Large,XX-Large\',
  3: \'Size:Large,Medium,Small,X-Large\',
  4: \'Size:26,28,30,32,34,36\'},
 \'MRP\': {0: \'Rs\\n1699\',
  1: \'Rs\\n3499\',
  2: \'Rs\\n1199\',
  3: \'Rs\\n2299\',
  4: \'Rs\\n1699\'},
 \'SellPrice\': {0: \'849\', 1: \'2449\', 2: \'599\', 3: \'1379\', 4: \'849\'},
 \'Discount\': {0: \'50% off\',
  1: \'30% off\',
  2: \'50% off\',
  3: \'40% off\',
  4: \'50% off\'},
 \'Category\': {0: \'Westernwear-Women\',
  1: \'Westernwear-Women\',
  2: \'Westernwear-Women\',
  3: \'Westernwear-Women\',
  4: \'Westernwear-Women\'}}

这是我在使用 df.head().to_dict() 时得到的

  • 你能告诉我们你现在使用的代码吗?
  • 您能否将df.head().to_dict() 的结果粘贴到您的问题中?
  • 添加上面的代码!
  • dropna() 不是就地操作(无论如何,默认情况下不是)。你需要做df = df.dropna(),或df.dropna(inplace=True)

标签: python pandas


【解决方案1】:

尝试这个;

df = pd.DataFrame({"col1":[12,20,np.nan,np.nan],
               "col2":[10,np.nan,np.nan,40]})


df1 = df.dropna()

# df;
   col1  col2
0  12.0  10.0
1  20.0   NaN
2   NaN   NaN
3   NaN  40.0

# df1;
   col1  col2
0  12.0  10.0

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2020-09-05
    • 1970-01-01
    • 2022-11-25
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多