我有以下pandas数据帧:
import pandas as pd
import numpy as np
d = {'age' : [21, 45, 45, 5],
'salary' : [20, 40, 10, 100]}
df = pd.DataFrame(d)
Run Code Online (Sandbox Code Playgroud)
并且想添加一个名为"is_rich"的额外列,该列根据他/她的工资来捕获一个人是否富裕.我找到了多种方法来实现这一目标:
# method 1
df['is_rich_method1'] = np.where(df['salary']>=50, 'yes', 'no')
# method 2
df['is_rich_method2'] = ['yes' if x >= 50 else 'no' for x in df['salary']]
# method 3
df['is_rich_method3'] = 'no'
df.loc[df['salary'] > 50,'is_rich_method3'] = 'yes'
Run Code Online (Sandbox Code Playgroud)
导致:
但是我不明白首选的方式是什么.根据您的应用,所有方法都同样好吗?
我有一个Pandas Dataframe,并希望将字典中的数据统一添加到我的数据帧中的所有行.目前我循环遍历字典并将值设置为我的新列.有没有更有效的方法来做到这一点?
# coding: utf-8
import pandas as pd
df = pd.DataFrame({'age' : [1, 2, 3],'name' : ['Foo', 'Bar', 'Barbie']})
d = {"blah":42,"blah-blah":"bar"}
for k,v in d.items():
df[k] = v
df
Run Code Online (Sandbox Code Playgroud) 我使用wheel在Pypi上发布了一个示例python包。我想使用本教程在我的 Conda 频道上发布包。
但是,当我运行时:
conda skeleton pypi rutgerhofstepythonpackage出现以下错误:
Warning, the following versions were found for rutgerhofstepythonpackage
0.0.1
0.0.2
0.1.1
Using 0.1.1
Use --version to specify a different version.
Leaving build/test directories:
Work: /opt/anaconda3/conda-bld/skeleton_1523284768777/work
Test: /opt/anaconda3/conda-bld/skeleton_1523284768777/test_tmp
Leaving build/test environments:
Test: source activate /opt/anaconda3/conda-bld/skeleton_1523284768777/_test_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho
Build: source activate /opt/anaconda3/conda-bld/skeleton_1523284768777/_build_env
Error: No source urls found for rutgerhofstepythonpackage
Run Code Online (Sandbox Code Playgroud) 我有一个带有“年份”列的熊猫数据框。但是,由于外部合并,某些行具有 np.NaN 值。pandas 中列的数据类型因此转换为 float64 而不是整数(整数不能存储 NaN?)。接下来,我想将数据帧存储在 postGreSQL 数据库上。为此,我使用:
df.to_sql()
Run Code Online (Sandbox Code Playgroud)
一切正常,但我的 postGreSQL 列现在类型为“双精度”,并且 np.NaN 值现在为 [null]。这一切都是有道理的,因为输入列类型是 float64 而不是整数类型。
我想知道是否有一种方法可以将结果存储在带有 [nans] 的整数类型列中。
例子笔记本
阿米的回答结果:
我有一个带有索引的数据框,我想将其存储在 postgresql 数据库中。为此我使用df.to_sql(table_name,engine,if_exists='replace', index=True,chunksize=10000)
pandas 数据帧中的索引列被复制到数据库,但未设置为主键。
有两种解决方案需要额外的步骤:
df.to_sql(schema=) 文档在摄取表后设置主键。询问:
ALTER TABLE table_name 添加主键(id_column_name)
有没有一种方法可以在不指定架构或更改表的情况下设置主键?
我的目标是将 AWS RDS 上运行的 postgreSQL 数据库中的表复制到 Amazone S3 上的 .csv 文件。为此,我使用 AWS 数据管道并找到了以下教程,但是当我按照所有步骤操作时,我的管道卡在了:"WAITING FOR RUNNER"请参阅屏幕截图。AWS文档指出:
确保为这些任务的 runningOn 或 workerGroup 字段设置有效值
但是字段“运行于”已设置。知道为什么这条管道被卡住了吗?
和我的定义文件:
{
"objects": [
{
"output": {
"ref": "DataNodeId_Z8iDO"
},
"input": {
"ref": "DataNodeId_hEUzs"
},
"name": "DefaultCopyActivity01",
"runsOn": {
"ref": "ResourceId_oR8hY"
},
"id": "CopyActivityId_8zaDw",
"type": "CopyActivity"
},
{
"resourceRole": "DataPipelineDefaultResourceRole",
"role": "DataPipelineDefaultRole",
"name": "DefaultResource1",
"id": "ResourceId_oR8hY",
"type": "Ec2Resource",
"terminateAfter": "1 Hour"
},
{
"*password": "xxxxxxxxx",
"name": "DefaultDatabase1",
"id": "DatabaseId_BWxRr",
"type": "RdsDatabase",
"region": "eu-central-1", …Run Code Online (Sandbox Code Playgroud) 我正在使用 Google bigquery 来分析 GDELT GKG 2.0数据集,并希望更好地了解如何基于主题(或 V2Themes)进行查询。该文件提到了“分类清单”表格,但到目前为止,我在寻找名单是不成功的。
以下 asesome博客提到您可以使用世界银行分类法等来缩小搜索范围。我的目标是找到所有提到“干旱/水太少”的项目,所有提到“洪水/水太多”的项目以及所有提到“质量差/水太脏”的项目,这些项目在子上具有地理匹配国家层面。
到目前为止,我已经能够得到一个不同主题的列表,但这并不广泛,我没有得到它的层次结构/结构。
SELECT
DISTINCT theme
FROM (
SELECT
GKGRECORDID,
locations,
REGEXP_EXTRACT(themes,r'(^.[^,]+)') AS theme,
CAST(REGEXP_EXTRACT(locations,r'^(?:[^#]*#){0}([^#]*)') AS NUMERIC) AS location_type,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){1}([^#]*)') AS location_fullname,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){2}([^#]*)') AS location_countrycode,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){3}([^#]*)') AS location_adm1code,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){4}([^#]*)') AS location_adm2code,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){5}([^#]*)') AS location_latitude,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){6}([^#]*)') AS location_longitude,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){7}([^#]*)') AS location_featureid,
REGEXP_EXTRACT(locations,r'^(?:[^#]*#){8}([^#]*)') AS location_characteroffset,
DocumentIdentifier
FROM
`gdelt-bq.gdeltv2.gkg_partitioned`,
UNNEST(SPLIT(V2Locations,';')) AS locations,
UNNEST(SPLIT(V2Themes,';')) AS themes
WHERE
_PARTITIONTIME >= "2018-08-20 00:00:00"
AND _PARTITIONTIME < "2018-08-21 00:00:00" )
WHERE
(location_type = …Run Code Online (Sandbox Code Playgroud) 是否可以使用 Apache Beam 的 Python SDK 读取地理空间数据源(例如 Shapefile、Geopackage)?
我找到了此页面,但不知道如何从那里获取它。
我有以下数据框:
d = {'histogram' : [[1,2],[3,4],[5,6]]}
df = pd.DataFrame(d)
Run Code Online (Sandbox Code Playgroud)
直方图的长度始终相同(本例中为 2)。
我想将“直方图”列转换为二维 numpy 数组以输入神经网络。首选输出是:
output_array = np.array(d["histogram"])
Run Code Online (Sandbox Code Playgroud)
IE:
array([[1, 2],
[3, 4],
[5, 6]])
Run Code Online (Sandbox Code Playgroud)
但是当我尝试时:
df["histogram"].to_numpy()
Run Code Online (Sandbox Code Playgroud)
结果是列表数组而不是数组的 numpy 数组:
array([list([1, 2]), list([3, 4]), list([5, 6])], dtype=object)
Run Code Online (Sandbox Code Playgroud)
这对于神经网络来说是有问题的,因为我必须指定尺寸/形状。
我尝试通过转换为 numpy 数组来解决问题:
df["histogram_arrays"] = df["histogram"].apply(lambda x: np.array(x))
df["histogram_arrays"].to_numpy()
Run Code Online (Sandbox Code Playgroud)
它返回数组的一维数组,而不是二维数组。
array([array([1, 2]), array([3, 4]), array([5, 6])], dtype=object)
Run Code Online (Sandbox Code Playgroud)
如何将直方图放入二维数组中?
pandas ×5
postgresql ×2
python ×2
apache-beam ×1
conda ×1
docker ×1
gdelt ×1
gis ×1
integer ×1
nan ×1
null ×1
numpy ×1
python-2.7 ×1
python-wheel ×1
sqlalchemy ×1