即使元素JSON数组零长度?(JSON array zero length even with elements?)
我从服务器获取JSON响应并创建一个Javascript对象。 结构是这样的:
var response = { key1:[], key2:[], key3:[], key4:[], key5:[] }
当请求完成时,
response
对象成功完成,如下所示:Object (*expandable): key1: Array[0] key2: Array[0] key3: Array[0] key4: Array[20] key5: Array[113]
现在稍后我想将这些信息存储到数据库中。 我已经创建了一个函数,并且我使用了
console.log
响应对象来确保它没问题(这里变得有趣 - 请参阅注释):function setupDatabase(){ console.log(response); // prints the response correctly (see response above) console.log(response.key5); //prints key5: Array[0]. If I expand the Array[0] all the elements are inside. console.log("key5: "+response.key5.length);//prints 0!! }
前3个键为0是正常的,因为没有为它们返回的元素。 其余2个都没问题。 为什么我会得到这个日志,而我在连续的同一个对象上运行3个
console.log
命令? 我错过了什么吗?I am getting a JSON response from a server and creating a Javascript object. The structure is this:
var response = { key1:[], key2:[], key3:[], key4:[], key5:[] }
When the request completes the
response
object is successfully completed like this:Object (*expandable): key1: Array[0] key2: Array[0] key3: Array[0] key4: Array[20] key5: Array[113]
Now later on I want to store the information into a database. I have created a function and I
console.log
the response object to make sure it's ok (here it is getting interesting - see comments):function setupDatabase(){ console.log(response); // prints the response correctly (see response above) console.log(response.key5); //prints key5: Array[0]. If I expand the Array[0] all the elements are inside. console.log("key5: "+response.key5.length);//prints 0!! }
It's normal for the first 3 keys to be 0 because there are no elements returned for them. The rest 2 are ok. Why do I get this log, while I run 3
console.log
commands on the same object in a row? Am I missing something?
原文:https://stackoverflow.com/questions/30255173
最满意答案
不知道你为什么要这样做...但是使用
itertuples
for row in df.itertuples(): if row.check: print('Yes') Yes Yes Yes Yes Yes Yes Yes
您可以使用
map
来映射您的值df.check.map({True: 'Yes', False: ''}) 0 1 2 3 Yes 4 5 6 7 Yes 8 Yes 9 Yes 10 Yes 11 Yes 12 Yes Name: check, dtype: object
甚至
assign
它assign
回专栏df.assign(mapped=df.check.map({True: 'Yes', False: ''})) Price check mapped 0 10 False 1 20 False 2 30 False 3 40 True Yes 4 30 False 5 20 False 6 30 False 7 40 True Yes 8 50 True Yes 9 60 True Yes 10 70 True Yes 11 80 True Yes 12 90 True Yes
您可以使用
pd.DataFrame.where
,它在第一个参数的计算结果为True
时保留值,在False
时用第二个参数填充。 如果未传递第二个参数,则放置np.nan
。df.Price.where(df.check, -99) 0 -99 1 -99 2 -99 3 40 4 -99 5 -99 6 -99 7 40 8 50 9 60 10 70 11 80 12 90 Name: Price, dtype: int64
姐妹方法是
mask
,反之亦然。 保持第一个参数为False
。 这是一个等同的陈述df.Price.mask(~df.check, -99) 0 -99 1 -99 2 -99 3 40 4 -99 5 -99 6 -99 7 40 8 50 9 60 10 70 11 80 12 90 Name: Price, dtype: int64
Not sure why you'd want to do this... but use
itertuples
for row in df.itertuples(): if row.check: print('Yes') Yes Yes Yes Yes Yes Yes Yes
You can map your values with
map
df.check.map({True: 'Yes', False: ''}) 0 1 2 3 Yes 4 5 6 7 Yes 8 Yes 9 Yes 10 Yes 11 Yes 12 Yes Name: check, dtype: object
And even
assign
it back to a columndf.assign(mapped=df.check.map({True: 'Yes', False: ''})) Price check mapped 0 10 False 1 20 False 2 30 False 3 40 True Yes 4 30 False 5 20 False 6 30 False 7 40 True Yes 8 50 True Yes 9 60 True Yes 10 70 True Yes 11 80 True Yes 12 90 True Yes
You can use
pd.DataFrame.where
which keeps the values when the first argument evaluates toTrue
and fills in with the second argument whenFalse
. If the second argument isn't passed,np.nan
is placed instead.df.Price.where(df.check, -99) 0 -99 1 -99 2 -99 3 40 4 -99 5 -99 6 -99 7 40 8 50 9 60 10 70 11 80 12 90 Name: Price, dtype: int64
The sister method is
mask
which does the opposite. Keeps where the first argument isFalse
. This is an equivalent statementdf.Price.mask(~df.check, -99) 0 -99 1 -99 2 -99 3 40 4 -99 5 -99 6 -99 7 40 8 50 9 60 10 70 11 80 12 90 Name: Price, dtype: int64
相关问答
更多-
jwilner的回应是现场。 我正在探索是否有更快的选择,因为根据我的经验,平面阵列的总和(奇怪)比计数快。 这段代码看起来更快: df.isnull().values.any() 例如: In [2]: df = pd.DataFrame(np.random.randn(1000,1000)) In [3]: df[df > 0.9] = pd.np.nan In [4]: %timeit df.isnull().any().any() 100 loops, best of 3: 14.7 ms p ...
-
如何检查Pandas Dataframe的真或假 - Python(How to check Pandas Dataframe for True or False - Python)[2023-12-09]
不知道你为什么要这样做...但是使用itertuples for row in df.itertuples(): if row.check: print('Yes') Yes Yes Yes Yes Yes Yes Yes 您可以使用map来映射您的值 df.check.map({True: 'Yes', False: ''}) 0 1 2 3 Yes 4 5 6 7 ... -
熊猫:如何检查pandas数据帧是否包含特定值?(Pandas: How can I check if a pandas dataframe contains a specific value?)[2022-07-18]
你的问题有点令人困惑,但假设你想知道你的数据框中是否至少有一个False,你可以简单地使用 mask = df.mycol == False mask.value_counts() mask.sum() mask.sum() > 0 所有人都会说实话 your question is a bit confusing, but assuming that you want to know whether there is at least one False in your dataframe, you c ... -
你正在做的是在字符串中使用@col执行实际查询,而不是绑定到该变量的值。 您可以使用字符串插值,例如: rhs_values_list = df1["RHS"].tolist() col = "shoe_size" query = "{} in @rhs_values_list".format(col) result_set = df2.query(relaxed_query) What you are doing is executing the actual query w ...
-
pd.DataFrame({'Keys': MyDict.keys(), 'Values': MyDict.values()}) 回报 Keys Values 0 key2 value2 1 key1 value1 pd.DataFrame({'Keys': MyDict.keys(), 'Values': MyDict.values()}) returns Keys Values 0 key2 value2 1 key1 value1
-
将整数列转换为布尔值: print (type(df.loc[0, 'like']))
df['liked'] = df['like'].astype(bool) 或者与1分配比较: df['liked'] = df['like'] == 1 如果1是字符串比较字符串'1' : print (type(df.loc[0, 'like'])) df['liked'] = df['like'] == '1' print (df) ... -
对于链条件使用& for and 。 如果hasParking是boolean == True应该省略。 True - 作为boolean和'True'作为string之间存在差异,我认为你需要删除''因为布尔列。 if ((data['federalState']=='C') & (data['hasParking'])).any(): #same as #if ((data['federalState']=='C') & (data['hasParking'] == True)).any(): 并且首 ...
-
您可以重新索引蒙版以使其具有与df相同的形状,然后使用df.mask : df.mask(mask.reindex(df.index, df.columns, fill_value=False), 999) Out: a b c spam 999 999 6 ham 1 999 7 此时,常规索引也应该有效: df[mask.reindex(df.index, df.columns, fill_value=False)] = 999 You can reind ...
-
# expand and map ids from IDList to NewID flat_ids = pd.DataFrame({ "NewID": pd.np.repeat(df2.NewID, df2.IDList.str.len().tolist()), "IDList": [x for l in df2.IDList for x in l] }).set_index("IDList").NewID # extract ids from flat ids using loc df ...
-
Python Pandas检查一个值是否在同一天发生了一次以上(Python Pandas check if a value occurs more then once in the same day)[2023-06-16]
我可能使用布尔数组索引。 我们想要删除行(如果我理解你所追求的,无论如何!),它们有yyy和多个dateuse / station组合。 我们可以使用transform来广播每个dateuse / station组合的大小,直到数据帧的长度,然后选择长度> 1的组中的行。然后我们可以& yyy的位置一起使用。 >>> multiple = df.groupby(["dateuse", "station"])["variable1"].transform(len) > 1 >>> must_be_isolat ...