小编Blu*_*ail的帖子

如何使用 ColumnTransformer() 返回数据框?

我有一个像这样的数据框:

department      review  projects salary satisfaction bonus  avg_hrs_month   left
0   operations  0.577569    3   low         0.626759    0   180.866070      0
1   operations  0.751900    3   medium      0.443679    0   182.708149      0
2   support     0.722548    3   medium      0.446823    0   184.416084      0
3   logistics   0.675158    4   high        0.440139    0   188.707545      0
4   sales       0.676203    3   high        0.577607    1   179.821083      0

Run Code Online (Sandbox Code Playgroud)

我想尝试 ColumnTransformer() 并返回转换后的数据帧。

ord_features = ["salary"]
ordinal_transformer = OrdinalEncoder()


cat_features = ["department"]
categorical_transformer = OneHotEncoder(handle_unknown="ignore")

ct = ColumnTransformer(
    transformers=[
        ("ord", ordinal_transformer, ord_features),
        ("cat", categorical_transformer, cat_features ), …
Run Code Online (Sandbox Code Playgroud)

encoder dataframe python-3.x scikit-learn

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

使用循环或 lambda 在多个数据框中添加具有相同字符串值的列的更有效方法?

我想在我的 8 个类似数据框中的每一个中添加一个新列 Category。此列中的值相同,它们也是 df 名称,如df1_p8本例中所示。我用过了:

In:     df61_p8.insert(3,"Category","df61_p8", True)
# or simply, df61_p8['Category']='df61_p8'

Out:
        code    violation_description                                            Category
89491   9-1-503 Defective or obstructed duct system one- building                df61_p8
102045  9-1-503 Defective or obstructed duct system one- building                df61_p8
103369  9-1-503 Defective or obstructed duct system one- building                df61_p8
130440  9-1-502 Failure to maintain at least one (1) elevator                    df61_p8
132446  9-1-503 Defective or obstructed duct system one-  building               df61_p8
Run Code Online (Sandbox Code Playgroud)

最终,我想将这 8 个数据帧附加/连接到一个数据帧中。

我想知道是否有更有效的方法来做到这一点,而不是在每个数据帧上一一使用 .insert 。像循环或 lambdas 之类的东西..作为初学者,我不确定如何在我的情况下应用它们?谢谢你。

append_alldfs = …
Run Code Online (Sandbox Code Playgroud)

python lambda loops python-3.x pandas

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

如何在 WSL2 上的 Ubuntu 中安装 cuDNN?

我在 Windows 10 上运行 Tensorflow 2 时遇到了问题(它只运行一个时期),因此我尝试通过 WSL2 在 Ubuntu 上安装 cuda 和 tensorflow 2。

我已遵循本指南: https ://ubuntu.com/tutorials/enabling-gpu-acceleration-on-ubuntu-on-wsl2-with-the-nvidia-cuda-platform#5-enjoy-ubuntu-on-wsl与此处的 Nvidia 非常相似,只是我安装了 cuda 版本 11.4 而不是最新的 cuda 11.7: https://docs.nvidia.com/cuda/wsl-user-guide/index.html#installing-wsl2

如果我需要在此之后安装 cuDNN 以及什么版本,我会陷入困境。就我而言,是 Windows 还是 Linux(https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html)?

当我运行笔记本时,出现以下问题,表明 cuDNN 丢失:

> physical_devices = tf.config.list_physical_devices('GPU')
> physical_devices
> 2022-06-03 18:30:28.966198: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory
> 2022-06-03 18:30:28.966233: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make …
Run Code Online (Sandbox Code Playgroud)

ubuntu python-3.x tensorflow

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

如何在 Seaborn FacetGrid 中设置刻度?

我有这样的代码,我想在 X 轴上添加刻度,这样我就可以更好地看到超过 150 的值对应的内容。我的 X 值的范围是从 178 到 17639。

bins = np.linspace(df.days_with_cr_line.min(), df.days_with_cr_line.max(), 32)

g = sns.FacetGrid(df, col="loan_status", hue="loan_status", palette=['#8856a7', '#f03b20'], col_wrap=2)
g.map(plt.hist, 'days_with_cr_line', bins=bins, ec="k")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我努力了

g.set_xticks(np.arange(0,18000,500), minor=True)
AttributeError: 'FacetGrid' object has no attribute 'set_xticks'
Run Code Online (Sandbox Code Playgroud)

for axes in g.axes.flat:
    _ = axes.set_xticks(axes.get_xticks(range(0,18000)))
Run Code Online (Sandbox Code Playgroud)

这将删除刻度标签而不添加任何刻度。

python seaborn facet-grid xticks

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

Extractall 日期 - 如何在 python 中使用 RegEx 分隔单个年份?

我在数据框中的一列中的测试中包含了一些日期。例如,

sr = pd.Series(['04/20/2009', '04/20/09', '4/20/09', '4/3/09', '6/2008','12/2009','2010'])
Run Code Online (Sandbox Code Playgroud)

我想提取这些日期......我一年中有一半的时间在“月”和“日”列中结束。

result = sr.str.extractall(r'(?P<month>\d{,2})[/]?(?P<day>\d{,2})[/]?(?P<year>\d{2,4})')
result

      month day year
  match         
0   0   04  20  2009
1   0   04  20  09
2   0   4   20  09
3   0   4   3   09
4   0   6   20  08
5   0   12  20  09
6   0   20  NaN 10

Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

我只能想到将“'6/2008'、'12/2009'、'2010'”与“'04/20/2009'、'04/20/09'、'4/20/09'”分开处理,然后附加它们。

regex date regex-group python-3.x pandas

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

如何查找 pandas 数据框中缺失的索引值?

我的数据框是这样的。我知道我在数据清理中丢失了一些行,因为 len(df) 以前是 500,现在是 489。例如,我可以看到第 496 行丢失了。


    all       month day year
0   03/25/93    03  25  93
...
480     2013    1   1   2013
481     1974    1   1   1974
482     1990    1   1   1990
483     1995    1   1   1995
484     2004    1   1   2004
485     1987    1   1   1987
486     1973    1   1   1973
487     1992    1   1   1992
488     1977    1   1   1977
489     1985    1   1   1985
490     2007    1   1   2007
491     2009    1   1   2009
492     1986    1 …
Run Code Online (Sandbox Code Playgroud)

indexing loops apply python-3.x pandas

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

如何将 pandas 数据框中的多列从字符串转换为整数?

我有一个数据框,其中包含包含数据的多个年份列。

df_all = pd.read_csv('../filename.csv', header=2, skiprows= range(38,120), 
                     encoding = "ISO-8859-1")

    Code    Persons    1981    1982        1983      1984        1985 ....
    S002    Angus      5,180   46,650      5,568     265,708     344,500
Run Code Online (Sandbox Code Playgroud)

我想将年份列(1981 到 2020)的数据从“object”转换为“int64”。

我努力了

df_all['1981'] = df_all['1981'].apply(np.int64)

df_all['1981'] = df_all['1981'].astype('int64')
Run Code Online (Sandbox Code Playgroud)

这导致了 ValueError: invalidliteral for int() with base 10: '5,180'

int(df_all['1981'])
TypeError: cannot convert the series to <class 'int'>

float(int(df_all['1981'])
TypeError: cannot convert the series to <class 'float'>
Run Code Online (Sandbox Code Playgroud)

所以我不知道为什么它甚至对于单个列也不起作用?

另外,有没有办法使用 cols=df_all.loc[:, '1981':'2019'] 一次将它们全部转换?

python dataframe pandas

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