在标题行为NaN的数据框中删除列的最pythonic地方是什么?最好是在位.
列中可能有也可能没有数据.
df = pd.DataFrame({'col1': [1,2,np.NaN], 'col2': [4,5,6], np.NaN: [7,np.NaN,9]})
df.dropna(axis='columns', inplace=True)
Run Code Online (Sandbox Code Playgroud)
在查看列中的数据时不这样做.
想要输出
df = pd.DataFrame({'col1': [1,2,np.NaN], 'col2': [4,5,6]})
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的回复.
这已经部分覆盖。
该dtype of df["D"] = np.nan在接受的答案是dtype=numpy.float64。
有没有办法在每个单元格中初始化一个空列表?
尝试过,df["D"] = [[]] * len(df)但是所有值都指向同一对象,将一个值设置为一个将全部设置。
df = pd.DataFrame({"A": [1,2,3], "B": [2,3,4]})
df
A B
0 1 2
1 2 3
2 3 4
df["D"] = [[]] * len(df)
df
A B D
0 1 2 []
1 2 3 []
2 3 4 []
df['D'][1].append(['a','b','c','d'])
df
A B D
0 1 2 [[a, b, c, d]]
1 2 3 [[a, b, c, d]]
2 3 4 …Run Code Online (Sandbox Code Playgroud) 考虑这个集合列表
my_input_list= [
{1,2,3,4,5},
{2,3,7,4,5},
set(),
{1,2,3,4,5,6},
set(),]
Run Code Online (Sandbox Code Playgroud)
我想获得唯一的独占元素6和7作为响应,列表或集合.设置首选.
我试过
print reduce(set.symmetric_difference,my_input_list)但是给了
{2,3,4,5,6,7}
Run Code Online (Sandbox Code Playgroud)
我尝试按长度排序列表,最小的首先由于两个空集引起错误.最大的第一个给出与未排序相同的结果.
有任何帮助或想法吗?谢谢 :)