gpt4 book ai didi

python - 如何将数据帧从长转换为宽,值在索引中按年份分组?

转载 作者:行者123 更新时间:2023-12-03 14:31:37 25 4
gpt4 key购买 nike

下面的代码与我以前使用的 csv 一起使用,两个 csv 的列数相同,并且列的名称相同。
有效 csv 的数据 here
没有的 csv 数据 here
这个错误是什么意思?为什么我收到这个错误?

from pandas import read_csv
from pandas import DataFrame
from pandas import Grouper
from matplotlib import pyplot

series = read_csv('carringtonairtemp.csv', header=0, index_col=0, parse_dates=True, squeeze=True)

groups = series.groupby(Grouper(freq='A'))
years = DataFrame()

for name, group in groups:
years[name.year] = group.values

years = years.T

pyplot.matshow(years, interpolation=None, aspect='auto')
pyplot.show()
错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-7173fcbe8c08> in <module>
6 # display(group.head())
7 # print(group.values[:10])
----> 8 years[name.year] = group.values

e:\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
3038 else:
3039 # set column
-> 3040 self._set_item(key, value)
3041
3042 def _setitem_slice(self, key: slice, value):

e:\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
3114 """
3115 self._ensure_valid_index(value)
-> 3116 value = self._sanitize_column(key, value)
3117 NDFrame._set_item(self, key, value)
3118

e:\Anaconda3\lib\site-packages\pandas\core\frame.py in _sanitize_column(self, key, value, broadcast)
3759
3760 # turn me into an ndarray
-> 3761 value = sanitize_index(value, self.index)
3762 if not isinstance(value, (np.ndarray, Index)):
3763 if isinstance(value, list) and len(value) > 0:

e:\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in sanitize_index(data, index)
745 """
746 if len(data) != len(index):
--> 747 raise ValueError(
748 "Length of values "
749 f"({len(data)}) "

ValueError: Length of values (365) does not match length of index (252)

最佳答案

  • 以所示方式迭代创建数据帧的问题是它需要新列匹配现有数据帧的长度,year , 指数。
  • 在较小的数据集中,所有年份都是 365 天,没有缺失天数。
  • 较大的数据集混合了 365 天和 366 天的年份,并且缺少 1990 年和 2020 年的数据,这导致 ValueError: Length of values (365) does not match length of index (252) .
  • 以下是一个更简洁的脚本,它实现了所需的数据框形状和绘图。
  • 这个实现没有不等数据长度的问题。


  • import pandas as pd
    import matplotlib.pyplot as plt

    # links to data
    url1 = 'https://raw.githubusercontent.com/trenton3983/stack_overflow/master/data/so_data/2020-09-19%20%2063975678/daily-min-temperatures.csv'
    url2 = 'https://raw.githubusercontent.com/trenton3983/stack_overflow/master/data/so_data/2020-09-19%20%2063975678/carringtonairtemp.csv'

    # load the data into a DataFrame, not a Series
    # parse the dates, and set them as the index
    df1 = pd.read_csv(url1, parse_dates=['Date'], index_col=['Date'])
    df2 = pd.read_csv(url2, parse_dates=['Date'], index_col=['Date'])

    # groupby year and aggregate Temp into a list
    dfg1 = df1.groupby(df1.index.year).agg({'Temp': list})
    dfg2 = df2.groupby(df2.index.year).agg({'Temp': list})

    # create a wide format dataframe with all the temp data expanded
    df1_wide = pd.DataFrame(dfg1.Temp.tolist(), index=dfg1.index)
    df2_wide = pd.DataFrame(dfg2.Temp.tolist(), index=dfg2.index)

    # plot
    fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 10))

    ax1.matshow(df1_wide, interpolation=None, aspect='auto')
    ax2.matshow(df2_wide, interpolation=None, aspect='auto')
    enter image description here

    关于python - 如何将数据帧从长转换为宽,值在索引中按年份分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63975678/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com