假设我有df
以下内容。我想合并价格列和价值列,以便所有价格都在一个列中,所有数量都在另一列中。我还想要第三列来确定价格水平。例如,unit1
、unit2
和unit3
。
import numpy as np
import pandas as pd
df = pd.DataFrame(
{
'uid': ['U100', 'U200', 'E100', 'E200', 'E300', 'A100', 'A200', 'A300', 'A400', 'A500'],
'location': ['US', 'US', 'EU', 'EU', 'EU', 'Asia', 'Asia', 'Asia', 'Asia', 'Asia'],
'unit1_price': [10, 20, 15, 10, 10, 10, 20, 20, 25, 25],
'unit1_vol': [100, 150, 100, 200, 150, 150, 100, 200, 200, 200],
'unit2_price': [10, 25, 30, 20, 10, 10, 10, 10, 20, 20],
'unit2_vol': [200, 200, 150, …
Run Code Online (Sandbox Code Playgroud) git init
git add file1.csv
git commit -m "First commit"
git remote add origin <Github url from Quick Setup page>
git push -u origin main
我收到以下错误:
error: src refspec main does not match any
error: failed to push some refs to <url>
我搜索了一个解决方案,我遇到了这个:git error: failed to push some refs to remote The answer selected 说:
如果 GitHub 存储库看到推送到它的新提交,而您在本地工作时,我建议使用:
git pull --rebase origin master
git push …
我的一列是以下格式的日期类型:YYYY-MM-DD。我想提取 YYYY-MM。到目前为止,我遇到的资源表明我可以使用提取任一年份,SELECT extract(year from order_date)...
但我不知道如何提取年份和月份。我尝试了以下方法,但没有成功:https ://www.w3schools.com/sql/func_mysql_extract.asp
我正在尝试将本地 Postgres 服务器连接到 AWS RDS 实例。当我输入凭据时:
我不断收到错误:无法连接到服务器:超时已过期
我查了一下,好像是安全组的问题。我执行了以下操作,但没有成功:https://serverfault.com/questions/656079/unable-to-connect-to-public-postgresql-rds-instance
给定以下数据框:
df_test = pd.DataFrame(
[[1, "BURGLARY"], [2, "PETIT LARCENY"], [3, "DANGEROUS DRUGS"], [4, "LOITERING FOR DRUG PURPOSES"], [5, "DANGEROUS WEAPONS"]],
columns = ['id','ofns_desc']
)
Run Code Online (Sandbox Code Playgroud)
我想添加一个新列来简化列中的描述ofns_desc
。我做了以下事情:
THEFT = ["BURGLARY", "PETIT LARCENY"]
df_test.loc[df_test.ofns_desc.isin(THEFT), 'category'] = "THEFT"
DRUGS = ["DANGEROUS DRUGS", "LOITERING FOR DRUG PURPOSES"]
df_test.loc[df_test.ofns_desc.isin(DRUGS), 'category'] = "DRUGS"
Run Code Online (Sandbox Code Playgroud)
到目前为止,上面的代码有效:
但是当我尝试"OTHER"
为该category
列创建一个值时,该列中的每个值category
都会被覆盖:
ALL_CAT = [THEFT, DRUGS]
df_test.loc[~df_test.ofns_desc.isin(ALL_CAT), 'category'] = "OTHER"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
假设我有以下数据帧:
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime as dt
df = pd.DataFrame(
[
['2008-02-19', 10],
['2008-03-01', 15],
['2009-02-05', 20],
['2009-05-10', 40],
['2010-10-10', 25],
['2010-11-15', 5]
],
columns = ['Date', 'DollarTotal']
)
df
Run Code Online (Sandbox Code Playgroud)
我想绘制按年份求和的总数,因此我执行以下转换:
df['Date'] = pd.to_datetime(df['Date'])
df_Year = df.groupby(df['Date'].dt.year)
df_Year = df_Year.sum('DollarTotal')
df_Year
Run Code Online (Sandbox Code Playgroud)
matplotlib 中的以下代码创建以下图表:
fig,ax = plt.subplots()
ax.plot(df_Year.index, df_Year.values)
ax.set_xlabel("OrderYear")
ax.set_ylabel("$ Total")
ax.set_title("Annual Purchase Amount")
plt.xticks([x for x in df_Year.index], rotation=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
该问题时,我想创建一个发生条形图使用相同的数据帧。通过将上面的代码从 更改ax.plot
为ax.bar
,我收到以下错误:
在 …
python ×3
dataframe ×2
pandas ×2
postgresql ×2
amazon-rds ×1
git ×1
github ×1
matplotlib ×1
python-3.x ×1
sql ×1
unpivot ×1