python原子数据类型(python atomic data types)
这里写的是Python有原子和引用对象类型。 原子对象是:int,long,complex。 分配原子对象时,会复制它的值,在分配引用对象时会复制它的引用。
我的问题是:为什么然后,当我做代码时,我得到'真'?
a = 1234 b = a print id(a) == id(b)
在我看来,我不复制价值,我只是复制参考,无论它是什么类型。
It was written here that Python has both atomic and reference object types. Atomic objects are: int, long, complex. When assigning atomic object, it's value is copied, when assigning reference object it's reference is copied.
My question is: why then, when i do the code bellow i get 'True'?
a = 1234 b = a print id(a) == id(b)
It seems to me that i don't copy value, i just copy reference, no matter what type it is.
原文:https://stackoverflow.com/questions/39125486
最满意答案
如果'列'在索引中。
In [217]: df.loc[(df.index.get_level_values('Site In') == 'Mid') | (df.index.get_level_values('Site Out') == 'Mid')] Out[217]: v Site In Site Out Transmission Commodity Mid North hvac Elec 1 South hvac Elec 1 North Mid hvac Elec 1 South Mid hvac Elec 1
如果它是一个扁平的数据帧,请使用
In [168]: df.loc[(df['Site In'] == 'Mid') | (df['Site Out'] == 'Mid')] Out[168]: Site In Site Out Transmission Commodity 0 Mid North hvac Elec 1 Mid South hvac Elec 2 North Mid hvac Elec 4 South Mid hvac Elec
要么
In [169]: df.loc[df['Site In'].eq('Mid') | df['Site Out'].eq('Mid')] Out[169]: Site In Site Out Transmission Commodity 0 Mid North hvac Elec 1 Mid South hvac Elec 2 North Mid hvac Elec 4 South Mid hvac Elec
If 'columns' are in index.
In [217]: df.loc[(df.index.get_level_values('Site In') == 'Mid') | (df.index.get_level_values('Site Out') == 'Mid')] Out[217]: v Site In Site Out Transmission Commodity Mid North hvac Elec 1 South hvac Elec 1 North Mid hvac Elec 1 South Mid hvac Elec 1
If it's a flat dataframe, use
In [168]: df.loc[(df['Site In'] == 'Mid') | (df['Site Out'] == 'Mid')] Out[168]: Site In Site Out Transmission Commodity 0 Mid North hvac Elec 1 Mid South hvac Elec 2 North Mid hvac Elec 4 South Mid hvac Elec
Or
In [169]: df.loc[df['Site In'].eq('Mid') | df['Site Out'].eq('Mid')] Out[169]: Site In Site Out Transmission Commodity 0 Mid North hvac Elec 1 Mid South hvac Elec 2 North Mid hvac Elec 4 South Mid hvac Elec
相关问答
更多-
我将假设你的目标是获得merged['val_x']并且你真的不关心merged的其他列。 以下是一些选项: def using_merge(df1, df2): merged = pd.merge(df1, df2, on=["obs", "year"], how="left") mask = ~np.isfinite(merged.val_x) merged.loc[mask, 'val_x'] = merged.loc[mask, 'val_y'] return mer ...
-
以下是不同数据结构中的信息: In [8]: df = pd.DataFrame({'cat1':[0,3,1], 'cat2':[2,0,1], 'cat3':[2,1,0]}) In [9]: df Out[9]: cat1 cat2 cat3 0 0 2 2 1 3 0 1 2 1 1 0 [3 rows x 3 columns] In [10]: rowmax = df.max(axis=1) 最大值由Tru ...
-
对所有dupe行使用参数duplicated with keep=False ,然后按所有列groupby并将索引值转换为元组,最后将输出Series转换为list : df = df[df.duplicated(keep=False)] df = df.groupby(df.columns.tolist()).apply(lambda x: tuple(x.index)).tolist() print (df) [(1, 6), (2, 4), (3, 5)] 如果你还想看到重写值: df1 = (d ...
-
Pandas:将数据帧插入其他数据帧而不保留索引(Pandas: Insert dataframe into other dataframe without preserving indices)[2022-12-04]
插入时确保将df更改为值,pandas是索引敏感的,这意味着在计算过程中它总是会尝试与索引和列匹配 original_df.iloc[[0,2],[0,1]] = df_to_insert.values original_df Out[651]: 0 1 2 0 10 11 3 1 4 5 6 2 12 13 9 When you do insert make sure change the df to values , pandas is index sensiti ... -
希望不言而喻,但如果可能的话,升级你的python / pandas! 在这种情况下,在最近的版本( 0.20.3 )中,我在两种情况下都会丢失值 - 我需要将查找键转换为日期时间,我猜它也适用于你。 基于便利字符串的日期索引仅适用于标量/切片。 In [174]: lookup = pd.to_datetime(filter1[filter1.columns[0]][0:2]) In [175]: df2.ix[lookup] Out[175]: t1 ...
-
过滤pandas DataFrame(Filtering pandas DataFrame)[2022-02-19]
如果要检查字典值: df_filtered = df[df.col4.isin(values.values())] 如果要检查字典键: df_filtered = df[df.col4.isin(values.keys())] If you want to check against the dictionary values: df_filtered = df[df.col4.isin(values.values())] If you want to check against the dictio ... -
如果'列'在索引中。 In [217]: df.loc[(df.index.get_level_values('Site In') == 'Mid') | (df.index.get_level_values('Site Out') == 'Mid')] Out[217]: v Site In Site Out Transmission Commodity Mid North ...
-
您希望为每个组获取索引。 它存储在groupbydataframe的'groups'属性中。 #filter for coverage==True #group by 'name' #access the 'groups' attribute by_person = df[df.coverage].groupby('name').groups 将返回: {'Jason': Int64Index([0, 5], dtype='int64'), 'Tina': Int64Index([4], dtype=' ...
-
您想将ignore_index=True传递给concat : In [68]: array1 = np.random.randn(3,3) array2 = np.random.randn(3,3) df1 = pd.DataFrame(array1, columns=list('ABC')) df2 = pd.DataFrame(array2, columns=list('ABC')) df = pd.concat([df1, df2], ignore_index=True) df Out[6 ...
-
您可以使用isin反转布尔Series ~ : import pandas as pd import numpy as np df = pd.DataFrame({'A' : [5,6,3,4,4,5,6,7,12,13], 'B' : [1,2,3,5,5,6,7,8,9,10,]}) print df A B 0 5 1 1 6 2 2 3 3 3 4 5 4 4 5 5 5 6 6 6 7 7 7 8 8 1 ...