如何从pandas系列中删除Freq: MS、Name: des、dtype: int64?

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

How to remove Freq: MS, Name: des, dtype: int64 from pandas series?

问题

以下是翻译好的部分:

我有一些像 `result_ses` 这样的pandas系列数据我想将所有数据累积到一个字典中并将数据保存在csv文件中我正在使用Google Colab进行工作但是我在删除数据中的一些不必要信息方面遇到了问题我的代码如下

asd = {}

for prod in unique_products[:4]:
    asd[prod] = {}  # 为每个产品创建一个空字典
    asd[prod]['ses'] = result_ses
    asd[prod]['des'] = result_des

print(asd)

输出如下

{'2-28-437': {'ses': 2021-05-01    16
                     2021-06-01    16
                     2021-07-01    16
                     Freq: MS, Name: ses, dtype: int64, 
              'des': 2021-05-01    14
                     2021-06-01    14
                     2021-07-01    13
                     Freq: MS, Name: des, dtype: int64}, 
  '2-2-329': {'ses': 2021-05-01    16
                   2021-06-01    16
                   2021-07-01    16
                   Freq: MS, Name: ses, dtype: int64, 
            'des': 2021-05-01    14
                   2021-06-01    14
                   2021-07-01    13
                   Freq: MS, Name: des, dtype: int64}, 
  '24-30-42-7400': {'ses': 2021-05-01    16
                           2021-06-01    16
                           2021-07-01    16
                           Freq: MS, Name: ses, dtype: int64, 
                    'des': 2021-05-01    14
                           2021-06-01    14
                           2021-07-01    13
                           Freq: MS, Name: des, dtype: int64}, 
  '2-53-1151': {'ses': 2021-05-01    16
                 2021-06-01    16
                 2021-07-01    16
                 Freq: MS, Name: ses, dtype: int64, 
          'des': 2021-05-01    14
                 2021-06-01    14
                 2021-07-01    13
                 Freq: MS, Name: des, dtype: int64}}

其中`result_ses``result_des`都是pandas系列,`unique_products`是一个字符串列表

# 如果我输入
result_ses.info() 
# 我得到
<class 'pandas.core.series.Series'>
DatetimeIndex: 3 entries, 2021-05-01 to 2021-07-01
Freq: MS
Series name: ses
Non-Null Count  Dtype
--------------  -----
3 non-null      int64
dtypes: int64(1)
memory usage: 48.0 bytes

要查看`result_ses`的内容我输入`print(result_ses)`并得到

2021-05-01    16
2021-06-01    16
2021-07-01    16
Freq: MS, Name: ses, dtype: int64 # 我不希望包含在csv文件中

我不希望字典`asd`包含这两个额外的信息具体为**Freq: MS, Name: des, dtype: int64**我只想保留其余部分以便我可以在csv文件中获得所需的输出使用以下代码我尝试将数据保存在csv文件中但它的格式不符合我的要求

op_path = '/content/output/'
output_file_path = op_path + f'desired_output.csv'
ddf = pd.DataFrame.from_dict(asd, orient='index')
ddf.to_csv(output_file_path, index_label='Date')

我希望最终输出的csv文件如下所示如何解决这个问题
[![desired output image in csv][1]][1]

请注意,这个翻译保留了您提供的代码和问题的完整内容,只翻译了代码和问题的描述。

英文:

I have some pandas series like result_ses. I wanted to accumulate all the data into a dictionary and save the data in a csv. I am using Google Colab to work. But I am facing trouble removing some of the unnecessary information from the data. My code is following:

asd = {}
for prod in unique_products[:4]:
asd[prod] = {}  # empty dictionary for each product
asd[prod][&#39;ses&#39;] = result_ses 
asd[prod][&#39;des&#39;] = result_des 
print(asd)

The output is following:

{&#39;2-28-437&#39;: {&#39;ses&#39;: 2021-05-01    16
2021-06-01    16
2021-07-01    16
Freq: MS, Name: ses, dtype: int64, 
&#39;des&#39;: 2021-05-01    14
2021-06-01    14
2021-07-01    13
Freq: MS, Name: des, dtype: int64}, 
&#39;2-2-329&#39;: {&#39;ses&#39;: 2021-05-01    16
2021-06-01    16
2021-07-01    16
Freq: MS, Name: ses, dtype: int64, 
&#39;des&#39;: 2021-05-01    14
2021-06-01    14
2021-07-01    13
Freq: MS, Name: des, dtype: int64}, 
&#39;24-30-42-7400&#39;: {&#39;ses&#39;: 2021-05-01    16
2021-06-01    16
2021-07-01    16
Freq: MS, Name: ses, dtype: int64, 
&#39;des&#39;: 2021-05-01    14
2021-06-01    14
2021-07-01    13
Freq: MS, Name: des, dtype: int64}, 
&#39;2-53-1151&#39;: {&#39;ses&#39;: 2021-05-01    16
2021-06-01    16
2021-07-01    16
Freq: MS, Name: ses, dtype: int64, 
&#39;des&#39;: 2021-05-01    14
2021-06-01    14
2021-07-01    13
Freq: MS, Name: des, dtype: int64}}

Where both the result_ses and result_des are pandas series and unique_products is a list of string.

# if I type
result_ses.info() 
# I get 
&lt;class &#39;pandas.core.series.Series&#39;&gt;
DatetimeIndex: 3 entries, 2021-05-01 to 2021-07-01
Freq: MS
Series name: ses
Non-Null Count  Dtype
--------------  -----
3 non-null      int64
dtypes: int64(1)
memory usage: 48.0 bytes

To view the contents of the result_ses I type print(result_ses) and get:

2021-05-01    16
2021-06-01    16
2021-07-01    16
Freq: MS, Name: ses, dtype: int64 # I do not want this included in the csv

I do not want the dictionary asd to include this two extra information specifically Freq: MS, Name: des, dtype: int64 and I want only the rest as it is so that I can get the desired output in the csv. Using the following code, I tried to save the data in the csv but it is not in the format I want.

op_path = &#39;/content/output/&#39;
output_file_path = op_path + f&#39;desired_output.csv&#39;
ddf = pd.DataFrame.from_dict(asd, orient=&#39;index&#39;)
ddf.to_csv(output_file_path, index_label=&#39;Date&#39;)

I am looking forward to getting the final output to be a csv like the following. How can I fix this problem?
如何从pandas系列中删除Freq: MS、Name: des、dtype: int64?

答案1

得分: 1

以下是翻译好的代码部分:

你可以通过`DatetimeIndex`创建列并为`ses``des`提供值

asd = {}
for prod in unique_products[:4]:
    asd[prod] = pd.DataFrame({'ses_date': result_ses.index, 
                              'ses_val': result_ses.to_numpy(),
                              'des_date': result_des.index, 
                              'des_val': result_des.to_numpy()})
df = pd.concat(asd)

要使用空格分隔不同的值

asd = {}
for prod in unique_products[:4]:
    ses = (result_ses.index.astype(str) + ' ' + result_ses.astype(str)).to_numpy()
    des = (result_des.index.astype(str) + ' ' + result_des.astype(str)).to_numpy()
    asd[prod] = pd.DataFrame({'ses': ses, 'des': des})
    
df = pd.concat(asd)

测试和解决方案运行良好

result_ses = pd.Series([1, 2, 3], pd.date_range('2000-01-01', periods=3))
result_des = pd.Series([8, 9, 7], pd.date_range('2000-02-01', periods=3))

unique_products = ['2-28-437', '2-2-329', '24-30-42-7400', '2-53-1151']
    
asd = {}
for prod in unique_products[:4]:
    ses = (result_ses.index.astype(str) + ' ' + result_ses.astype(str)).to_numpy()
    des = (result_des.index.astype(str) + ' ' + result_des.astype(str)).to_numpy()
    asd[prod] = pd.DataFrame({'ses': ses, 'des': des})
    
df = pd.concat(asd)
    
df.to_csv(f'modified_output.csv')

,,ses,des
2-28-437,0,2000-01-01 1,2000-02-01 8
2-28-437,1,2000-01-02 2,2000-02-02 9
2-28-437,2,2000-01-03 3,2000-02-03 7
2-2-329,0,2000-01-01 1,2000-02-01 8
2-2-329,1,2000-01-02 2,2000-02-02 9
2-2-329,2,2000-01-03 3,2000-02-03 7
24-30-42-7400,0,2000-01-01 1,2000-02-01 8
24-30-42-7400,1,2000-01-02 2,2000-02-02 9
24-30-42-7400,2,2000-01-03 3,2000-02-03 7
2-53-1151,0,2000-01-01 1,2000-02-01 8
2-53-1151,1,2000-01-02 2,2000-02-02 9
2-53-1151,2,2000-01-03 3,2000-02-03 7

如果需要在Excel中按独立行格式化

result_ses = pd.Series([1, 2, 3], pd.date_range('2000-01-01', periods=3))
result_des = pd.Series([8, 9, 7], pd.date_range('2000-02-01', periods=3))
unique_products = ['2-28-437', '2-2-329', '24-30-42-7400', '2-53-1151']
    
asd = {}
for prod in unique_products[:4]:
    ses = (result_ses.index.astype(str) + ' ' + result_ses.astype(str)).to_numpy()
    des = (result_des.index.astype(str) + ' ' + result_des.astype(str)).to_numpy()
    asd[prod] = pd.DataFrame({'ses': ses, 'des': des})
    
df = pd.concat(asd).groupby(level=0).agg('\n'.join).rename_axis('Date').reset_index()
    
with pd.ExcelWriter('modified_output.xlsx', engine='xlsxwriter') as writer:
    df.to_excel(writer, sheet_name='Sheet1', index=False)
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']
    cell_format = workbook.add_format({'text_wrap': True})
    worksheet.set_column('A:Z', cell_format=cell_format)

请注意,以上翻译只包括代码部分,不包括注释和问题描述。

英文:

You can create columns by DatetimeIndex and values for ses and des:

asd = {}
for prod in unique_products[:4]:
asd[prod] = pd.DataFrame({&#39;ses_date&#39;:result_ses.index, 
&#39;ses_val&#39;:result_ses.to_numpy(),
&#39;des_date&#39;:result_des.index, 
&#39;des_val&#39;:result_des.to_numpy()})
df = pd.concat(asd)

For separate values by space use:

asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) +&#39; &#39; + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str)  +&#39; &#39; + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({&#39;ses&#39;:ses, &#39;des&#39;:des})
df = pd.concat(asd)

Testing and solution working nice:

result_ses = pd.Series([1,2,3], pd.date_range(&#39;2000-01-01&#39;, periods=3))
result_des = pd.Series([8,9,7], pd.date_range(&#39;2000-02-01&#39;, periods=3))
unique_products = [&#39;2-28-437&#39;,&#39;2-2-329&#39;, &#39;24-30-42-7400&#39;, &#39;2-53-1151&#39;]
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) +&#39; &#39; + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str)  +&#39; &#39; + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({&#39;ses&#39;:ses, &#39;des&#39;:des})
df = pd.concat(asd)
df.to_csv(f&#39;modified_output.csv&#39;)
,,ses,des
2-28-437,0,2000-01-01 1,2000-02-01 8
2-28-437,1,2000-01-02 2,2000-02-02 9
2-28-437,2,2000-01-03 3,2000-02-03 7
2-2-329,0,2000-01-01 1,2000-02-01 8
2-2-329,1,2000-01-02 2,2000-02-02 9
2-2-329,2,2000-01-03 3,2000-02-03 7
24-30-42-7400,0,2000-01-01 1,2000-02-01 8
24-30-42-7400,1,2000-01-02 2,2000-02-02 9
24-30-42-7400,2,2000-01-03 3,2000-02-03 7
2-53-1151,0,2000-01-01 1,2000-02-01 8
2-53-1151,1,2000-01-02 2,2000-02-02 9
2-53-1151,2,2000-01-03 3,2000-02-03 7

If need format with separate lines in Excel:

result_ses = pd.Series([1,2,3], pd.date_range(&#39;2000-01-01&#39;, periods=3))
result_des = pd.Series([8,9,7], pd.date_range(&#39;2000-02-01&#39;, periods=3))
unique_products = [&#39;2-28-437&#39;,&#39;2-2-329&#39;, &#39;24-30-42-7400&#39;, &#39;2-53-1151&#39;]
asd = {}
for prod in unique_products[:4]:
ses = (result_ses.index.astype(str) +&#39; &#39; + result_ses.astype(str)).to_numpy()
des = (result_des.index.astype(str)  +&#39; &#39; + result_des.astype(str)).to_numpy()
asd[prod] = pd.DataFrame({&#39;ses&#39;:ses, &#39;des&#39;:des})
df = pd.concat(asd).groupby(level=0).agg(&#39;\n&#39;.join).rename_axis(&#39;Date&#39;).reset_index()
with pd.ExcelWriter(&#39;modified_output.xlsx&#39;, engine=&#39;xlsxwriter&#39;) as writer:
df.to_excel(writer, sheet_name=&#39;Sheet1&#39;, index=False)
workbook  = writer.book
worksheet = writer.sheets[&#39;Sheet1&#39;]
cell_format = workbook.add_format({&#39;text_wrap&#39;: True})
worksheet.set_column(&#39;A:Z&#39;, cell_format=cell_format)

huangapple
  • 本文由 发表于 2023年8月10日 17:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874167.html
匿名

发表评论

匿名网友

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

确定