声纳违例:安全 - 数组直接存储(Sonar Violation: Security - Array is stored directly)
有声音违规:
声纳违例:安全 - 数组直接存储
public void setMyArray(String[] myArray) { this.myArray = myArray; }
解:
public void setMyArray(String[] newMyArray) { if(newMyArray == null) { this.myArray = new String[0]; } else { this.myArray = Arrays.copyOf(newMyArray, newMyArray.length); } }
但我不知道为什么?
There is a Sonar Violation:
Sonar Violation: Security - Array is stored directly
public void setMyArray(String[] myArray) { this.myArray = myArray; }
Solution:
public void setMyArray(String[] newMyArray) { if(newMyArray == null) { this.myArray = new String[0]; } else { this.myArray = Arrays.copyOf(newMyArray, newMyArray.length); } }
But I wonder why ?
原文:https://stackoverflow.com/questions/11580948
最满意答案
当然,像大多数Python对象一样,您可以将新属性附加到
pandas.DataFrame
:import pandas as pd df = pd.DataFrame([]) df.instrument_name = 'Binky'
但请注意,尽管可以将属性附加到DataFrame,但是对DataFrame执行的操作(例如
groupby
,pivot
,join
或loc
仅举几例)可能会返回一个新的DataFrame, 而不附加元数据。 熊猫还没有一种可靠的传播 附加到DataFrames的元数据的方法。保存文件中的元数据是可能的。 您可以在这里找到如何在HDF5文件中存储元数据的示例。
Sure, like most Python objects, you can attach new attributes to a
pandas.DataFrame
:import pandas as pd df = pd.DataFrame([]) df.instrument_name = 'Binky'
Note, however, that while you can attach attributes to a DataFrame, operations performed on the DataFrame (such as
groupby
,pivot
,join
orloc
to name just a few) may return a new DataFrame without the metadata attached. Pandas does not yet have a robust method of propagating metadata attached to DataFrames.Preserving the metadata in a file is possible. You can find an example of how to store metadata in an HDF5 file here.
相关问答
更多-
当然,像大多数Python对象一样,您可以将新属性附加到pandas.DataFrame : import pandas as pd df = pd.DataFrame([]) df.instrument_name = 'Binky' 但请注意,尽管可以将属性附加到DataFrame,但是对DataFrame执行的操作(例如groupby , pivot , join或loc仅举几例)可能会返回一个新的DataFrame, 而不附加元数据。 熊猫还没有一种可靠的传播 附加到DataFrames的元数据的方 ...
-
In [16]: def shuffle(df, n=1, axis=0): ...: df = df.copy() ...: for _ in range(n): ...: df.apply(np.random.shuffle, axis=axis) ...: return df ...: In [17]: df = pd.DataFrame({'A':range(10), 'B':range(10)}) ...
-
每个列的确切代码将会有所不同,但您可能需要使用map和apply函数。 在某些情况下,您可以直接使用现有的列进行计算,因为这些列是Pandas Series对象,它们也可以作为Numpy数组运行,该数组对于常规的数学运算而言,它可以自动为单元而工作。 >>> d A B C 0 11 13 5 1 6 7 4 2 8 3 6 3 4 8 7 4 0 1 7 >>> (d.A + d.B) / d.C 0 4.800000 1 3.250 ...
-
使用iloc就像你在使用ix ,但应用不同的切片... df2 = df1.iloc[3:] #edited since .ix is now deprecated. 会给你一个没有前三行的新df。 Use iloc: df = df.iloc[3:] will give you a new df without the first three rows.
-
您可以使用df.empty属性来检查它是否为空: if df.empty: print('DataFrame is empty!') 资料来源: 熊猫文献 You can use the attribute df.empty to check whether it's empty or not: if df.empty: print('DataFrame is empty!') Source: Pandas Documentation
-
尝试这个: def merge_dicst(dlist): d = {} for x in dlist: d.update(x) return d In [97]: pd.DataFrame([merge_dicst(x) for x in all_meta]) Out[97]: ERROR Person b-setting trial 0 NaN 1 Off 2 1 NaN 2 ...
-
让我们尝试set_index并add : fwdTempTable.set_index('monthID', append=True).add(monthlyAdder, fill_value=0).reset_index('monthID') 输出: monthID City1 City2 City3 City4 DateTime 2017-1-01 1 22.01 24.1 ...
-
扩大熊猫数据框架(Broaden pandas dataframe)[2022-11-30]
您可以使用groupby作为list ,然后使用DataFrame构造函数: a = df.groupby('Box')['Code'].apply(list) df = pd.DataFrame(a.values.tolist(), index=a.index).add_prefix('Code').reset_index() print (df) Box Code0 Code1 0 Blue 9820 4530 1 Green 1221 8391 2 Red ... -
大熊猫数据框预测价值(pandas dataframe look ahead value)[2023-04-03]
如何使用diff计算差异,然后使用下一个非零值返回填充0: import pandas as pd import numpy as np df = pd.DataFrame({"S": [13, 13, 13, 14, 13, 12]}) df.S.diff(-1).replace(0, np.nan).bfill() # replace zero with nan and apply back fill. # 0 -1 # 1 -1 # 2 -1 # 3 1 # 4 ... -
多头大熊猫数据帧(multiheader pandas dataframe)[2022-11-14]
我认为您可以通过.to_csv()执行此操作,因为此方法接受文件路径(如您所做)或缓冲区。 我假设您知道元标题,元数据和属性标题字符串的顺序,因此您可以选择将其写入文件的方式。 您缺少的部分如下所示。 with open('output.csv','w') as fid: # write your meta header etc., here assumed to be a list of strings fid.write(','.join(meta_header) + '\n') ...