小编Ato*_*ore的帖子

在这种情况下(Pandas)如何解决 KeyError(f"None of [{key}] are in the [{axis_name}]")?

我有一个 CSV 文件,例如如下所示:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n
ID姓名电子邮件物理化学数学
1斯塔sta@example.com677890
2丹尼dany@example.com779889
3埃勒elle@example.com776790
\n
\n

现在我想使用 pandas 输出一个新的 CSV 文件,它也有新的列,例如如下所示:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ID姓名年级地址物理化学出席率数学全部的
\n
\n

我想在随机位置创建新列,并且希望将新列中的值设置为空白。

\n

我尝试过使用:

\n
import pandas as pd\n\ndf = pd.read_csv("sample.csv")\nfinal_df = df.loc[[\'id\',\'name\',\'grade\',\'address\',\'physics\',\'chemistry\',\'attendance\',\'maths\',\'total\']]\n
Run Code Online (Sandbox Code Playgroud)\n

当我这样做时,我收到一个错误:

\n
KeyError(f\xe2\x80\x9cNone of [{key}] are in the [{axis_name}]\xe2\x80\x9d)\n
Run Code Online (Sandbox Code Playgroud)\n

有任何想法或建议来安排这个。

\n

python numpy dataframe python-3.x pandas

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

如何修复生产中的 502 Bad Gateway 错误(Nginx)?

当我尝试在数字海洋中托管的项目中上传大小约为 600MB 的大 csv 文件时,它尝试上传但显示 502 Bad Gateway Error (Nginx)。

该应用程序是一个数据转换应用程序。

这在本地工作时效果很好。

sudo tail -30 /var/log/nginx/error.log
Run Code Online (Sandbox Code Playgroud)

节目

[error] 132235#132235: *239 upstream prematurely closed connection while reading response header from upstream, client: client's ip , server: ip, request: "POST /submit/ HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/submit/", host: "ip", referrer: "http://ip/"

sudo nano /etc/nginx/sites-available/myproject
Run Code Online (Sandbox Code Playgroud)

节目

server {
    listen 80;
    server_name ip;
    client_max_body_size 999M;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
       alias  /root/static/;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock; …
Run Code Online (Sandbox Code Playgroud)

python django nginx gunicorn nginx-config

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

如何填充列表中的值并将其转换为数据帧?

我想对我之前的问题进行一些修改:

从列和数据框迭代条件到列表转换(熊猫)

数据框是:

 Item   Quantity  Price     Photo1     Photo2    Photo3    Photo4

A        2         30      A1.jpg      A2.jpg 
B        4         10      B1.jpg      B2.jpg    B3.jpg    B4.jpg
C        5         15      C1.jpg
Run Code Online (Sandbox Code Playgroud)

我试过:

df1 = df.reindex(['Item','Quantity','Price','Photo1','Photo2','Photo3','Photo4','I','Q','P','PH',] axis=1)
df1['I'] = df1['I'].fillna['I']
df1['Q'] = df1['Q'].fillna['Q']
df1['P'] = df1['P'].fillna['P']
df1['PH'] = df1['PH'].fillna['PH']
vals = [['I','Item'],['Q','Quantity'],['P','Price']]

photo_df = df1.filter(like='Photo')
photo_df = photo_df.transform(lambda x: np.where(x.isnull(), x, x.name)) 
photo_df = photo_df.fillna('')

vals = [y for x in photo_df.to_numpy() 
         for y in vals[:3] + [['PH',z] for z in x[x!='']] ]
Run Code Online (Sandbox Code Playgroud)

vals 返回:

[['I', …
Run Code Online (Sandbox Code Playgroud)

python numpy list python-3.x pandas

5
推荐指数
0
解决办法
105
查看次数

如何为内连接创建 django 查询集对象?

我有两张桌子。

class DibbsSpiderDibbsMatchedProductFieldsDuplicate(models.Model):
    nsn = models.TextField()
    nsn2 = models.TextField()
    cage = models.TextField()
    part_number = models.TextField()
    company_name = models.TextField(blank=True, null=True)
    supplier = models.TextField(db_column='Supplier', blank=True, null=True)  # Field name made lowercase.
    cost = models.CharField(db_column='Cost', max_length=15, blank=True, null=True)  # Field name made lowercase.
    list_price = models.CharField(db_column='List_Price', max_length=15, blank=True, null=True)  # Field name made lowercase.
    gsa_price = models.CharField(db_column='GSA_Price', max_length=15, blank=True, null=True)  # Field name made lowercase.
    hash = models.TextField()
    nomenclature = models.TextField()
    technical_documents = models.TextField()
    solicitation = models.CharField(max_length=32)
    status = models.CharField(max_length=16)
    purchase_request = models.TextField() …
Run Code Online (Sandbox Code Playgroud)

python django django-queryset django-views

5
推荐指数
1
解决办法
1899
查看次数

如何根据指定的条件将数据帧划分为新的数据帧?

我有一个数据框:例如:

df =

Questions             Answers

Where is Amazon?       Brazil

Is he a scientist?         No

Did he stole my money?    Yes

What does your father do?  Business

He is a great player.      I don't think so.
 
She is my girlfriend.      I too agree.
Run Code Online (Sandbox Code Playgroud)

我想在条件中从上述数据帧创建三个数据帧:

df1 的条件:

如果 df['Questions'] 的第一个单词来自列表:

# list of Yes/No verbs
yn_list = ['Do','Does','Did','do','does','did','Am','Are','Is','Was','Were','am','are','is','was','were',
           'Have','Has','Had','have','has','had','Will','Would','Shall','Should','Can','Could','May',
           'Might','will','would','shall','should','can','could','may','might']

# list of negative Yes/No verbs
yn_negative_list = ["Don't","Doesn't","Didn't","don't","doesn't","didn't","Aren't","Isn't","aren't","isn't",
                    "Wasn't","Weren't","wasn't","weren't","Haven't","Hasn't","Hadn't","haven't","hasn't",
                    "hadn't","Won't","Wouldn't","won't","wouldn't","Shan't","shan't","Shouldn't","Can't",
                    "Couldn't","shouldn't","can't","couldn't","may not","May not","Mightn't","mightn't"]
Run Code Online (Sandbox Code Playgroud)

df2 的条件:

如果 df['Questions'] 的第一个单词来自列表:

wh_list = ['who','where','what','when','why','whom','which','whose','how'] …
Run Code Online (Sandbox Code Playgroud)

python list dataframe python-3.x pandas

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