私信  •  关注

ansev

ansev 最近创建的主题
ansev 最近回复了
5 年前
回复了 ansev 创建的主题 » Python:如何使用regex拆分列中的值并保留右边?

IIUC公司

df['Size'] = df['Size'].str.split(r'[A-Z]', n=1).str[-1]
#if neccessary astype(str)
#df['Size'] = df['Size'].astype(str).str.split(r'[A-Z]', n=1).str[-1]

df['Size'] = df['Size'].str.split(r'[A-Z]', n=1, expand = True).ffill(axis=1)[1]

print(df)
    Product Size
0  product1  255
1  product2    2
2  product3  500

使用 DataFrame.pivot_table 为了得到索引 GroupBy.cumcount

new_df = (df.pivot_table(index=df.groupby('Stat').cumcount(),
                         columns='Stat',
                         values='Value',
                         aggfunc=''.join)
             .rename_axis(columns=None)
             .sort_index(ascending=False ,axis=1)
         )
print(new_df)

输出

      name loca IDnu
0   cobras   DC    2
1  pythons   LA    1

我会用 pdcrosstab

pd.crosstab(df['user'], df['income'].isnull())[True].sort_values(ascending = False)
#user
#Alice    1
#Bob      1
#Jane     1
#Name: True, dtype: int64

GroupBy.sum

df['income'].isnull().groupby(df['user']).sum().astype(int).sort_values(ascending=False)
#user
#Alice    1
#Bob      1
#Jane     1
#Name: income, dtype: int64

注意

True

5 年前
回复了 ansev 创建的主题 » 在其中一个列中加入不同的列-python

不是所有的空白都是 '' ,所以我们可以使用 pd.to_numeric 具有 errors = 'coerce' 然后填写 NaN 价值观 Series.fillna .

df['col1'] = pd.to_numeric(df['col1'],errors = 'coerce').fillna(df['col2'])
5 年前
回复了 ansev 创建的主题 » 在python中获取列表的某些值

使用 DtatetimeIndex.strftime :

mylist = df.Volatilidad_14[df.Volatilidad_14 == df['Volatilidad_14'].min()].index.strftime('%Y-%m-%d').tolist()