小编Rtu*_*tut的帖子

pandas.dataframe.query() 中的模式搜索

我有以下数据框:

----------------------------
Index| col1 | col2 |
----------------------------
0    | 1    | a-b-c

1    | 2    | d-e-f

2    | 3    |  g
----------------------------
Run Code Online (Sandbox Code Playgroud)

我希望能够进行如下查询:

myvar= 'a'
df.query('@myvar in col2')
Run Code Online (Sandbox Code Playgroud)

但它总是适用于精确匹配。模式匹配有什么解决方案吗?

谢谢,

鲁特

python pattern-matching pandas

9
推荐指数
2
解决办法
9774
查看次数

Pandas.dataframe.query() - 获取非空行(Pandas等效于SQL:"IS NOT NULL")

我使用以下代码从pandas dataframe获取带有一些值的行.我需要将此代码转换为pandas.query().

results= rs_gp[rs_gp['Col1'].notnull()]
Run Code Online (Sandbox Code Playgroud)

当我转换为: results= rs_gp.query('Col1!=None')

它给出了错误'None is not defined'.有人可以帮忙吗

谢谢,Rtut

python dataframe pandas

8
推荐指数
2
解决办法
3477
查看次数

Pandas数据框通过逗号将重复列组合成一个单独的数据

我的数据框有很少的重复列名.如果找到重复的列名称,请将重复列合并为一列.我还想保留用逗号分隔的重复列数据.任何人都可以建议一种方法来做到这一点.

我在下面构建了一个例子.在我的实际数据帧中,列名称未知.

输入数据框架:

  Col1 Col2 Col3 Col2
A  CA1  CA5  CA3  CA5
B  CB1  CB5  CB3  CB5
C  CC1  CC5  CC3  CC5
D  CD1  CD5  CD3  None
E  CE1  CE5  CE3  CE5
Run Code Online (Sandbox Code Playgroud)

可以阅读:

df = pd.read_clipboard(names=['Col1','Col2','Col3','Col2'], skiprows=1)
Run Code Online (Sandbox Code Playgroud)

输出数据框架:

  Col1     Col2 Col3
A  CA1  CA5,CA5  CA3
B  CB1  CB5,CB5  CB3
C  CC1  CC5,CC5  CC3
D  CD1  CD5  CD3
E  CE1  CE5,CE5  CE3
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

4
推荐指数
2
解决办法
2289
查看次数

Pandas pd.isnull()函数

我需要将数据框中的非空值替换为1,将null值替换为0.

这是我的数据帧:

my_list= [['a','b','c'],['test1','test2',None],[None,'101','000']]

mydf= pd.DataFrame(my_list,columns=['col1','col2','col3'])

mydf

    col1   col2  col3
0      a      b     c
1  test1  test2  None
2   None    101   000

mydf.where((pd.isnull(mydf)),0,inplace=True)

mydf

   col1 col2  col3
0     0    0     0
1     0    0  None
2  None    0     0
Run Code Online (Sandbox Code Playgroud)

我不确定为什么它用零替换非空值.pd.notnull()恰恰相反.任何人都能解释我在这里缺少的东西吗?

python pandas

3
推荐指数
1
解决办法
1万
查看次数

熊猫to_excel-如何使其更快

我有一个具有12,000行和34列的数据框。熊猫大约需要15秒才能将其写入Excel。我读到的关于to_excel函数的讨论很少,而使其更快的一种方法是添加engine ='xlsxwriter'。我使用以下代码。

writer = pd.ExcelWriter('outputfile.xlsx',engine='xlsxwriter')
res_df.to_excel(writer,sheet_name='Output_sheet')
Run Code Online (Sandbox Code Playgroud)

想知道是否有一种方法可以使用dask或任何其他库使此工作更快?

dataframe.memory_usage()给了我以下输出:

Index   80
col1    95528
col2    95528
col3    95528
col4    95528
col5    95528
col6    95528
col7    95528
col8    95528
col9    95528
col10   95528
col11   95528
col12   95528
col13   95528
col14   95528
col15   95528
col16   95528
col17   95528
col18   95528
col19   95528
col20   95528
col21   95528
col22   95528
col23   95528
col24   95528
col25   95528
col26   95528
col27   95528
col28   95528
col29   95528
col30   95528
col31   95528
col32   95528
col33   95528
col34   95528
Run Code Online (Sandbox Code Playgroud)

谢谢!

performance export-to-excel pandas

3
推荐指数
1
解决办法
3632
查看次数

获得大熊猫数据帧组的中位数

我希望能够计算数据帧中列组的中位数.我有以下示例数据帧.对于我的实际数据帧,列数,列和组的名称是动态的,因为它取决于用户输入.

raw_data= {'a':['g1','g2','g3','g4','g5'],'b':[10,11,12,13,14],'c':[5,6,7,8,9],'d':[112,1,0,9,8],'e':[6,7,8,0,9],'f':[0,1,6,7,8],'g':[9,8,6,5,4]}
mydf= pd.DataFrame(raw_data)
newdf= mydf.set_index('a')
Run Code Online (Sandbox Code Playgroud)

我有另一个字典,我存储我的列的组信息

gp_dict= {'gp1':['b','c','d'],'gp2':['e','f','g']}
Run Code Online (Sandbox Code Playgroud)

当前数据帧:

     b  c    d  e  f  g
a                      
g1  10  5  112  6  0  9
g2  11  6    1  7  1  8
g3  12  7    0  8  6  6
g4  13  8    9  0  7  5
g5  14  9    8  9  8  4
Run Code Online (Sandbox Code Playgroud)

我的代码应该获得每个组的中位数,并且如果任何组的中位数大于8,则保留行.

期望的输出:

     b  c    d  e  f  g
g1  10  5  112  6  0  9
g4  13  8    9  0  7  5
g5  14  9    8  9 …
Run Code Online (Sandbox Code Playgroud)

python pandas

2
推荐指数
1
解决办法
1050
查看次数