小编Fil*_*tio的帖子

groupby中的pandas聚合函数-默认选项?

我有以下数据集 ( df)。我想groupby使用品牌作为我的索引,获取工人和价值列的平均值以及提供者列的第一个计数。

brand   workers value   provider
H&M      322    56         mark
H&M      450    433        mark
Lindex  678     233        luke
Lindex  543     456        luke
Levi    234     32         chris
Levi    789     12         chris
Run Code Online (Sandbox Code Playgroud)

现在我能

df = df.groupby('brand')['workers', 'value', 'provider'].agg({'workers': mean,  'value':mean, 'provider' : first).reset_index()
Run Code Online (Sandbox Code Playgroud)

但考虑到我的真实数据集作为更多列的方式,我想取平均值并且我不想指定每个列,有没有更好的方法来声明默认函数?

有点“取所有非字符串列的平均值和字符串列的第一个观察值?”

python group-by dataframe pandas pandas-groupby

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

创建两个分类变量的热图

我有以下三个变量的数据集:

  1. df['Score'] 浮动虚拟(1 或 0)
  2. df['Province'] 一个对象列,其中每一行是一个区域
  3. df['Product type'] 指示行业的对象。

我想创建一个联合图,在 x 轴上我有不同的行业,在 y 轴上是不同的省份,作为我的联合图的颜色,我有得分的相对频率。像这样的东西。 https://seaborn.pydata.org/examples/hexbin_marginals.html

暂时我只能做到以下几点

mean = df.groupby(['Province', 'Product type'])['score'].mean()
Run Code Online (Sandbox Code Playgroud)

但我不确定如何绘制它。

谢谢!

python plot pandas

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

Pandas:找到 pandas 数据框中最左边的值,后跟全 1

我有以下数据集

data = {'ID': ['A', 'B', 'C', 'D'], 
        '2012': [0, 1, 1, 1], 
        '2013': [0, 0, 1, 1], 
        '2014': [0, 0, 0, 1], 
        '2015': [0, 0, 1, 1], 
        '2016': [0, 0, 1, 0], 
        '2017': [1, 0, 1,1]}

df  = pd.DataFrame(data)
Run Code Online (Sandbox Code Playgroud)

对于每一行,我想生成一个新列 - Baseline_Year- 它假定右侧所有值都等于 1 的列的名称。如果没有所有值都等于 1 的列,我Baseline_Year希望等于失踪。

查看预期结果

data = {'ID': ['A', 'B', 'C', 'D', 'E'], 
        '2012': [0, 1, 1, 1, 1], 
        '2013': [0, 0, 1, 1, 1], 
        '2014': [0, 0, 0, 1, 1], 
        '2015': [0, …
Run Code Online (Sandbox Code Playgroud)

python row sum pandas

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

批量翻译的 IAM 权限问题:PermissionDenied:403 Cloud IAM 权限“cloudtranslate.generalModels.batchPredict”被拒绝

这是我第一次运行谷歌批量翻译,我通常运行API翻译,但这次我的文件太大,无法以这种方式翻译。

我尝试运行以下脚本,但收到下面的权限错误,我不知道如何继续。任何帮助深表感谢..

from google.cloud import translate

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= r"C:\Users\..my_path..\cred.json"
# translate_client = translate.Client()

def batch_translate_text(
    input_uri="gs://....",
    output_uri="gs://....",
    project_id="....",
    timeout=180):
    """Translates a batch of texts on GCS and stores the result in a GCS location."""

    client = translate.TranslationServiceClient()

    location = "us-central1"
    # Supported file types: https://cloud.google.com/translate/docs/supported-formats
    gcs_source = {"input_uri": input_uri}

    input_configs_element = {
        "gcs_source": gcs_source,
        "mime_type": "text/plain",  # Can be "text/plain" or "text/html".
    }
    gcs_destination = {"output_uri_prefix": output_uri}
    output_config = {"gcs_destination": gcs_destination}
    parent = f"projects/{project_id}/locations/{location}"

    # Supported language codes: https://cloud.google.com/translate/docs/language
    operation = …
Run Code Online (Sandbox Code Playgroud)

python translation google-cloud-platform

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