如何在pandas中基于链式操作分配新列

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

How to assign new columns based on chaining in pandas

问题

我正在尝试在pandas中使用链式操作创建一个新的数据框。

  1. result = (
  2. df.drop(['Day_of_year', 'Month', 'Week_of_year'], axis='columns'),
  3. pd.to_datetime(df['timestamp']),
  4. .assign("random" = 0)
  5. )
  6. # 访问元组的第一个元素
  7. updated_df = result[0]
  8. updated_df
  9. 如果我注释掉最后一行元组中的代码可以正常工作但我想要分配新的列
  10. 我该如何做到这一点
英文:

I'm trying to create a new dataframe using chaining in pandas.

  1. result = (
  2. df.drop(['Day_of_year', 'Month', 'Week_of_year'], axis='columns'),
  3. pd.to_datetime(df['timestamp']),
  4. .assign("random" = 0)
  5. )
  6. # Access the first element of the tuple
  7. updated_df = result[0]
  8. updated_df

If I comment out the last line the code in the tuple work but I want to assign new columns.

How do I do this?

答案1

得分: 1

尝试这个:

  1. updated_df = (
  2. df.drop(columns=['Day_of_year', 'Month', 'Week_of_year'])
  3. .assign(**{"random": 0, 'timestamp': pd.to_datetime(df['timestamp'])})
  4. )

测试

在一个虚拟数据框上评估上述代码:

  1. import pandas as pd
  2. # 创建一个日期范围
  3. date_range = pd.date_range(start='2023-07-01', end='2023-07-10')
  4. # 创建数据框
  5. df = pd.DataFrame()
  6. df['timestamp'] = date_range
  7. df['Day_of_year'] = df['timestamp'].dt.dayofyear
  8. df['Month'] = df['timestamp'].dt.month
  9. df['Week_of_year'] = df['timestamp'].dt.isocalendar().week
  10. updated_df = (
  11. df.drop(columns=['Day_of_year', 'Month', 'Week_of_year'])
  12. .assign(**{"random": 0, 'timestamp': pd.to_datetime(df['timestamp'])})
  13. )
  14. print(updated_df)
  15. # 打印:
  16. #
  17. # timestamp random
  18. # 0 2023-07-01 0
  19. # 1 2023-07-02 0
  20. # 2 2023-07-03 0
  21. # 3 2023-07-04 0
  22. # 4 2023-07-05 0
  23. # 5 2023-07-06 0
  24. # 6 2023-07-07 0
  25. # 7 2023-07-08 0
  26. # 8 2023-07-09 0
  27. # 9 2023-07-10 0
英文:

Try this:

  1. updated_df = (
  2. df.drop(columns=['Day_of_year', 'Month', 'Week_of_year'])
  3. .assign(**{"random": 0, 'timestamp': pd.to_datetime(df['timestamp'])})
  4. )

Testing

Evaluating the above code on a dummy dataframe:

  1. import pandas as pd
  2. # Create a date range
  3. date_range = pd.date_range(start='2023-07-01', end='2023-07-10')
  4. # Create the DataFrame
  5. df = pd.DataFrame()
  6. df['timestamp'] = date_range
  7. df['Day_of_year'] = df['timestamp'].dt.dayofyear
  8. df['Month'] = df['timestamp'].dt.month
  9. df['Week_of_year'] = df['timestamp'].dt.isocalendar().week
  10. updated_df = (
  11. df.drop(columns=['Day_of_year', 'Month', 'Week_of_year'])
  12. .assign(**{"random": 0, 'timestamp': pd.to_datetime(df['timestamp'])})
  13. )
  14. print(updated_df)
  15. # Prints:
  16. #
  17. # timestamp random
  18. # 0 2023-07-01 0
  19. # 1 2023-07-02 0
  20. # 2 2023-07-03 0
  21. # 3 2023-07-04 0
  22. # 4 2023-07-05 0
  23. # 5 2023-07-06 0
  24. # 6 2023-07-07 0
  25. # 7 2023-07-08 0
  26. # 8 2023-07-09 0
  27. # 9 2023-07-10 0

huangapple
  • 本文由 发表于 2023年7月7日 01:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631090.html
匿名

发表评论

匿名网友

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

确定