我有一个 CSV 文件,例如如下所示:
\n| ID | 姓名 | 电子邮件 | 物理 | 化学 | 数学 |
|---|---|---|---|---|---|
| 1 | 斯塔 | sta@example.com | 67 | 78 | 90 |
| 2 | 丹尼 | dany@example.com | 77 | 98 | 89 |
| 3 | 埃勒 | elle@example.com | 77 | 67 | 90 |
现在我想使用 pandas 输出一个新的 CSV 文件,它也有新的列,例如如下所示:
\n| ID | 姓名 | 年级 | 地址 | 物理 | 化学 | 出席率 | 数学 | 全部的 |
|---|
我想在随机位置创建新列,并且希望将新列中的值设置为空白。
\n我尝试过使用:
\nimport pandas as pd\n\ndf = pd.read_csv("sample.csv")\nfinal_df = df.loc[[\'id\',\'name\',\'grade\',\'address\',\'physics\',\'chemistry\',\'attendance\',\'maths\',\'total\']]\nRun Code Online (Sandbox Code Playgroud)\n当我这样做时,我收到一个错误:
\nKeyError(f\xe2\x80\x9cNone of [{key}] are in the [{axis_name}]\xe2\x80\x9d)\nRun Code Online (Sandbox Code Playgroud)\n有任何想法或建议来安排这个。
\n当我尝试在数字海洋中托管的项目中上传大小约为 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) 我想对我之前的问题进行一些修改:
数据框是:
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) 我有两张桌子。
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) 我有一个数据框:例如:
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 ×5
pandas ×3
python-3.x ×3
dataframe ×2
django ×2
list ×2
numpy ×2
django-views ×1
gunicorn ×1
nginx ×1
nginx-config ×1