可以在创建新数据集时使用if else函数吗?

huangapple go评论100阅读模式
英文:

Python: Can I use if else function when creating a new dataset?

问题

I'm a beginner in python.

我是Python的初学者。

I need to find out what function should I use in order to create a new dataset (df2) for empty original dataset (df). For example,

我需要找出在空的原始数据集(df)上应该使用什么函数来创建一个新的数据集(df2)。例如,

  1. df2 = df['count_days'].dt.days >= 1

当我的数据集中没有需要计算的天数时(空数据),我会得到一个错误。

当我的数据集中没有需要计算的天数时(空数据),我会得到一个错误。

Can I use if else statement to resolve this issue? For example:

我可以使用if else语句来解决这个问题吗?例如:

  1. if df['count_days'].dt.days >= 1:
  2. df2
  3. else:
  4. print("No Data")

BTW - I sometimes would have empty dataset because there is no record available.

顺便说一下 - 有时我的数据集会为空,因为没有可用的记录。

Thank you!

谢谢!

英文:

I'm a beginner in python.

I need to find out what function should I use in order to create a new dataset (df2) for empty original dataset (df). For example,

  1. df2 = df['count_days'].dt.days >= 1

When I don't have no days to count in the dataset (empty data), I get an error.

Can I use if else statement to resolve this issue?
For example:

  1. if df['count_days'].dt.days >= 1
  2. df2
  3. else
  4. print("No Data")

BTW - I sometimes would have empty dataset because there is no record available.

Thank you!

答案1

得分: 2

是的,您可以使用if和else语句:

  1. import pandas as pd
  2. if df.empty:
  3. print("没有数据")
  4. else:
  5. df2 = df['count_days'].dt.days >= 1
英文:

Yes you can use the if and else statements:

  1. import pandas as pd
  2. if df.empty:
  3. print("No Data")
  4. else:
  5. df2 = df['count_days'].dt.days >= 1

答案2

得分: 1

  1. import pandas as pd
  2. df = pd.DataFrame({'count_days': pd.Series([], dtype='timedelta64[ns]')})
  3. df['count_days'].dt.days >= 1 # 无错误
英文:

Instead of working around it, you could create a dataframe which doesn't produce these errors:

  1. import pandas as pd
  2. df = pd.DataFrame({'count_days': pd.Series([], dtype='timedelta64[ns]')})
  3. df['count_days'].dt.days >= 1 # no error
  4. </details>
  5. # 答案3
  6. **得分**: 0
  7. 如果您需要填充数据,通常在需要填充不存在的数据时使用 [NaN `np.nan`(或等效的 "not a number")](https://en.wikipedia.org/wiki/NaN) 或 `.nat`("not a time") - 这是 [IEEE 754 规范](https://en.wikipedia.org/wiki/IEEE_754) 的一部分。
  8. ```python
  9. &gt;&gt;&gt; df = pd.DataFrame({&#39;a&#39;:[1,2],&#39;b&#39;:[3,4]})
  10. &gt;&gt;&gt; df
  11. a b
  12. 0 1 3
  13. 1 2 4
  14. &gt;&gt;&gt; df[&quot;c&quot;] = np.nan # NumPy
  15. &gt;&gt;&gt; df
  16. a b c
  17. 0 1 3 NaN
  18. 1 2 4 NaN
  19. &gt;&gt;&gt; pd.merge(df, pd.DataFrame({&#39;a&#39;:[5],&#39;b&#39;:[6],&#39;c&#39;:[7]}), &#39;outer&#39;)
  20. a b c
  21. 0 1 3 NaN
  22. 1 2 4 NaN
  23. 2 5 6 7.0
  24. &gt;&gt;&gt; df[df[&#39;c&#39;].isna()] # 过滤 NaN 值
  25. a b c
  26. 0 1 3 NaN
  27. 1 2 4 NaN
  28. &gt;&gt;&gt; df[~df[&#39;c&#39;].isna()]
  29. a b c
  30. 2 5 6 7.0

在 Python 中,如果您有一个需要存在但不执行任何操作的对象,通常使用 None 是正确的对象。

  1. df = None
  2. ... # 中间逻辑
  3. if df is None: # 特殊情况
  4. ...
英文:

If you need to fill data, NaN np.nan (or equivalent "not a number") or .nat ("not a time") are frequently used when you need to fill data that does not exist - this is part of IEEE 754 specification

  1. &gt;&gt;&gt; df = pd.DataFrame({&#39;a&#39;:[1,2],&#39;b&#39;:[3,4]})
  2. &gt;&gt;&gt; df
  3. a b
  4. 0 1 3
  5. 1 2 4
  6. &gt;&gt;&gt; df[&quot;c&quot;] = np.nan # NumPy
  7. &gt;&gt;&gt; df
  8. a b c
  9. 0 1 3 NaN
  10. 1 2 4 NaN
  11. &gt;&gt;&gt; pd.merge(df, pd.DataFrame({&#39;a&#39;:[5],&#39;b&#39;:[6],&#39;c&#39;:[7]}), &#39;outer&#39;)
  12. a b c
  13. 0 1 3 NaN
  14. 1 2 4 NaN
  15. 2 5 6 7.0
  16. &gt;&gt;&gt; df[df[&#39;c&#39;].isna()] # filter NaNs
  17. a b c
  18. 0 1 3 NaN
  19. 1 2 4 NaN
  20. &gt;&gt;&gt; df[~df[&#39;c&#39;].isna()]
  21. a b c
  22. 2 5 6 7.0

In Python, if you have an object that you need to exist, but do nothing, frequently None is the correct object

  1. df = None
  2. ... # intermediate logic
  3. if df is None: # special case
  4. ...

huangapple
  • 本文由 发表于 2023年7月13日 22:30:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定