【问题标题】:python pandas: removing name and dtype in iterrowspython pandas:删除iterrows中的名称和dtype
【发布时间】:2022-01-08 13:01:28
【问题描述】:

我目前正在研究库存系统。 如何删除 itrows 中的“Name:,dtype:”? 还是有其他方法可以显示该行的信息? 谢谢

1 Name                  cat
Unit Price          $2.00
Quantity                2
Price               $4.00
Expiry Date    0002-02-02
Expired?               No
Name: 1, dtype: object
2 Name                    2
Unit Price          $2.00
Quantity                2
Price               $4.00
Expiry Date    2003-02-02
Expired?               No
Name: 2, dtype: object
3 Name                  dog
Unit Price          $2.00
Quantity                2
Price               $4.00
Expiry Date    0002-02-02
Expired?               No
Name: 3, dtype: object
4 Name                mouse
Unit Price          $3.00
Quantity                3
Price               $9.00
Expiry Date    0003-03-03
Expired?               No
Name: 4, dtype: object
5 Name                    3
Unit Price          $3.00
Quantity                3
Price               $9.00
Expiry Date    0003-03-03
Expired?               No
Name: 5, dtype: object```

【问题讨论】:

标签: python pandas


【解决方案1】:

您应该在打印前使用Series.to_string 方法:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,10,(3,4)))

for name,s in df.iterrows():
    print(f'row: {name}')
    print(s.to_string())  # to_string outputs only the data, not the metadata

示例:

row: 0
0    9
1    0
2    9
3    5
row: 1
0    1
1    2
2    5
3    6
row: 2
0    0
1    1
2    5
3    5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 2014-10-17
    • 2015-06-21
    • 2019-01-02
    • 2017-05-18
    相关资源
    最近更新 更多