小编pyd*_*pyd的帖子

将 Pandas DataFrame 转换为类似字节的对象

嗨,我正在尝试将我的 df 转换为二进制并将其存储在一个变量中。

我的_df:

 df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
Run Code Online (Sandbox Code Playgroud)

我的代码:

 import io
 towrite = io.BytesIO()
 df.to_excel(towrite)  # write to BytesIO buffer
 towrite.seek(0)  # reset pointer
 
Run Code Online (Sandbox Code Playgroud)

我正进入(状态 AttributeError: '_io.BytesIO' object has no attribute 'write_cells'

完整追溯:

AttributeError                            Traceback (most recent call last)
<ipython-input-25-be6ee9d9ede6> in <module>()
      1 towrite = io.BytesIO()
----> 2 df.to_excel(towrite)  # write to BytesIO buffer
      3 towrite.seek(0)  # reset pointer
      4 encoded = base64.b64encode(towrite.read())  #

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, …
Run Code Online (Sandbox Code Playgroud)

binary pickle bytesio pandas

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

在python中为列表中的项生成词云

 my_list=["one", "one two", "three"]
Run Code Online (Sandbox Code Playgroud)

我正在使用这个列表生成一个文字云

 wordcloud = WordCloud(width = 1000, height = 500).generate(" ".join(my_list))
Run Code Online (Sandbox Code Playgroud)

当我将所有项目转换为字符串时,它正在生成单词云

   "one","two","three"

 But I want to generate word cloud for the values, "one","one two","three"
Run Code Online (Sandbox Code Playgroud)

帮助我为列表中的项目生成文字云

python string list cpu-word word-cloud

7
推荐指数
1
解决办法
6731
查看次数

python f'string 在 pd.Series.map 函数中不起作用

我有一个pd系列,

s = pd.Series([1, 2, 3, np.nan])
Run Code Online (Sandbox Code Playgroud)

当我做,

s.map('this is a string {}'.format)
[out]
0    this is a string 1.0
1    this is a string 2.0
2    this is a string 3.0
3    this is a string nan   
Run Code Online (Sandbox Code Playgroud)

如何使用格式化字符串获得相同的结果?

s.map(f'this is a string {?}') ?
Run Code Online (Sandbox Code Playgroud)

python format series pandas f-string

6
推荐指数
1
解决办法
1264
查看次数

`error:unbalanced括号`,同时检查项目是否存在于pandas数据帧中

df=pd.DataFrame({"A":["one","two","three"],"B":["fopur","give","six"]})
Run Code Online (Sandbox Code Playgroud)

当我做,

df.B.str.contains("six").any()
out[2]=True
Run Code Online (Sandbox Code Playgroud)

当我做,

df.B.str.contains("six)").any()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误,

C:\ProgramData\Anaconda3\lib\sre_parse.py in parse(str, flags, pattern)
    868     if source.next is not None:
    869         assert source.next == ")"
--> 870         raise source.error("unbalanced parenthesis")
    871 
    872     if flags & SRE_FLAG_DEBUG:

error: unbalanced parenthesis at position 3
Run Code Online (Sandbox Code Playgroud)

请帮忙!

python regex data-analysis dataframe pandas

5
推荐指数
2
解决办法
1051
查看次数

从python中的项目列表中删除特殊字符

my_list = ["on@3", "two#", "thre%e"]
Run Code Online (Sandbox Code Playgroud)

我的预期输出是

out_list = ["one","two","three"]
Run Code Online (Sandbox Code Playgroud)

我不能简单地申请strip()这些项目,请帮忙。

python string list special-characters

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

如何使用pandas从word文档(.docx)文件中的表创建数据框

我有一个带有数据表的word文件(.docx),我正在尝试使用该表创建一个pandas数据框,我使用了docx和pandas模块.但我无法创建数据框.

from docx import Document
document = Document('req.docx')
for table in document.tables:
    for row in table.rows:       
        for cell in row.cells:        
            print (cell.text)
Run Code Online (Sandbox Code Playgroud)

并尝试将表读为df pd.read_table("path of the file")

我可以逐个单元格读取数据,但我想读取整个表格或任何特定的列.提前致谢

python docx dataframe pandas python-docx

4
推荐指数
1
解决办法
4909
查看次数

如何使用python解码编码的excel文件

我的 Java 程序员将一个 excel 文件转换为二进制文件并将二进制内容发送给我。

他使用sun.misc.BASE64Encodersun.misc.BASE64Decoder()进行编码。

我需要使用 python 将该二进制数据转换为数据帧。

数据看起来像,

UEsDBBQABgAIAAAAIQBi7p1oXgEAAJAEAAATAAgCW0NvbnRlbnRfVHl........

我试过bas64解码器但没有帮助。

我的代码:

import base64
with open('encoded_data.txt','rb') as d:
    data=d.read()
print(data)
`UEsDBBQABgAIAAAAIQBi7p1oXgEAAJAEAAATAAgCW0NvbnRlbnRfVHl........`
decrypted=base64.b64decode(data)
print(decrypt)
  'PK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x00!\x00b\xee\x9dh^\x01\x00\x00\x90\x04\x00\x00\x13\x00\x08\x02[Content_Types].xml \xa2\x04\x02(\xa0\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
Run Code Online (Sandbox Code Playgroud)

请帮我将此二进制数据转换为熊猫数据框。

python java base64 encoding pandas

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

如何减少 Dash DataTable 的宽度

我有一个如下所示的 dash.datatable,但列宽对于我的值来说太长,列宽应限制为相应列的最大长度字。

我的代码:

app = JupyterDash(__name__)

df = pd.DataFrame(
{
    "A" : ["one","two","three"],
    "B" : ["one","two","three"],
    "C" : ["one","two","three"],
    
}
)
app.layout = dash_table.DataTable(
    data=df.to_dict('records'),
    columns=[{'id': c, 'name': c} for c in df.columns],
    style_data={
        'whiteSpace': 'normal',
        'height': 'auto',
    },
    
)

if __name__ == "__main__":
    app.run_server(mode="jupyterlab")
Run Code Online (Sandbox Code Playgroud)

我的数据表看起来像:

在此处输入图片说明

python datatable plotly-dash

4
推荐指数
1
解决办法
2253
查看次数

在python中使用pandas检索数据列上的匹配字数

我有一个 df,

Name      Description
Ram Ram   is one of the good cricketer
Sri Sri   is one of the member
Kumar     Kumar is a keeper
Run Code Online (Sandbox Code Playgroud)

和一个列表,my_list=["one","good","ravi","ball"]

我试图从 my_list 中获取至少有一个关键字的行。

我试过,

  mask=df["Description"].str.contains("|".join(my_list),na=False)
Run Code Online (Sandbox Code Playgroud)

我得到了 output_df,

Name    Description
Ram     Ram is one of ONe crickete
Sri     Sri is one of the member
Ravi    Ravi is a player, ravi is playing
Kumar   there is a BALL
Run Code Online (Sandbox Code Playgroud)

我还想添加“描述”中存在的关键字及其在单独列中的计数,

我想要的输出是,

Name    Description                      pre-keys          keys     count
Ram     Ram is one of ONe crickete         one,good,ONe   one,good    2
Sri     Sri …
Run Code Online (Sandbox Code Playgroud)

python data-analysis dataframe pandas

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

如何检查数据框中是否存在值

嗨,我想获取包含特定单词的数据框的列名,

例如:我有一个数据帧,

NA              good    employee
Not available   best    employer
not required    well    manager
not eligible    super   reportee

my_word=["well"]
Run Code Online (Sandbox Code Playgroud)

如何检查df中是否存在"井",以及"井"中的列名称是否存在

提前致谢!

python data-analysis dataframe pandas

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