Pyt*_*sta 1 python dataframe pandas
学习列比较。如何基于两列创建新列?
我可以做两种情况水果或蔬菜。但是对于第三个条件,做不到。:(
df
basket1 basket2
0 fruit fruit
1 vegetable vegetable
2 vegetable both
3 fruit both
Run Code Online (Sandbox Code Playgroud)
结果
纽迪夫
basket1 basket2 total
0 fruit fruit fruit
1 vegetable vegetable vegetable
2 vegetable both Unknown
3 fruit both fruit
Run Code Online (Sandbox Code Playgroud)
非常感谢你的帮助!
更新
重新审视这个,DataFrame.apply是缓慢的 AF。让我们看看其他一些选项,然后进行比较。
DataFrame.applynumpy.where当我们只有两种选择时,可以应用这种方法。在您的情况下,这是真的,因为我们返回df.awhendf.a == df.b或df.a == 'fruit' and df.b == 'both'。语法是np.where(condition, value_if_true, value_if_false).
In [42]: df['np_where'] = np.where(
...: ((df.a == df.b) | ((df.a == 'fruit') & (df.b == 'both'))),
...: df.a,
...: 'Unknown'
...: )
Run Code Online (Sandbox Code Playgroud)
numpy.select如果您有多个条件,您将使用此选项。其语法是np.select(condition, values, default)wheredefault是一个可选参数。
In [43]: conditions = df.a == df.b, (df.a == 'fruit') & (df.b == 'both')
In [44]: choices = df['a'], df['a']
In [45]: df['np_select'] = np.select(conditions, choices, default='Unknown')
Run Code Online (Sandbox Code Playgroud)
请注意,为了演示目的,即使结果产生相同的结果,我也创建了两个条件。
如您所见,所有三种方法都有相同的结果。
In [47]: df
Out[47]:
a b np_where np_select df_apply
0 fruit fruit fruit fruit fruit
1 vegetable vegetable vegetable vegetable vegetable
2 vegetable both Unknown Unknown Unknown
3 fruit both fruit fruit fruit
Run Code Online (Sandbox Code Playgroud)
但它们在速度方面如何比较?为了检查这一点,让我们创建一个更新、更大的DataFrame. 我们这样做是为了看看我们的选项如何处理大量数据。
In [48]: df_large = pd.DataFrame({
...: 'a': np.random.choice(['fruit', 'vegetable'], size=1_000_000),
...: 'b': np.random.choice(['fruit', 'vegetable', 'both'], size=1_000_000)
...: })
In [49]: %timeit df_large['np_where'] = np.where(((df_large.a == df_large.b) | ((df_large.a == 'fruit')
...: & (df_large.b == 'both'))), df_large.a, 'Unknown')
379 ms ± 64.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [50]: %timeit df_large['np_select'] = np.select(((df_large.a == df_large.b), ((df_large.a == 'fruit'
...: ) & (df_large.b == 'both'))), (df_large.a, df_large.a), default='Unknown')
580 ms ± 101 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [51]: %timeit df_large['df_apply'] = df_large.apply(total, axis=1)
40.5 s ± 6 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
Run Code Online (Sandbox Code Playgroud)
哇!正如您所看到的DataFrame.apply,它比我们的其他两个选项慢得多并且np.where边缘化了np.select。
np.where,如果你只有两种选择np.select如果您有多种选择,请使用DataFrame.apply(特别是对于大数据集)!创建自己的函数并使用 DataFrame.apply
In [104]: def total(r):
...: if r.a == r.b:
...: return r.a
...: elif (r.a == 'fruit') and (r.b == 'both'):
...: return r.a
...: return 'Unknown'
...:
In [105]: df = pd.DataFrame({'a': ['fruit', 'vegetable', 'vegetable', 'fruit'], 'b': ['fruit', 'vegetable', 'both', 'both']})
In [106]: df
Out[106]:
a b
0 fruit fruit
1 vegetable vegetable
2 vegetable both
3 fruit both
In [107]: df['total'] = df.apply(total, axis=1)
In [108]: df
Out[108]:
a b total
0 fruit fruit fruit
1 vegetable vegetable vegetable
2 vegetable both Unknown
3 fruit both fruit
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3940 次 |
| 最近记录: |