【问题标题】:How to assign integer value from an array to an array of string如何将数组中的整数值分配给字符串数组
【发布时间】:2021-10-13 16:32:58
【问题描述】:
import csv
import numpy as np
import pandas as pd

csv_file = 'Bruv.csv'

names = []
Bar = []
Price = []

with open(csv_file, 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        names.append(row.get('Sizes'))
        Bar.append(row.get('Product'))
        Price.append(row.get('Price'))

GG = pd.DataFrame()
GG['Sizes'] = names
GG['Bars'] = Bar
GG['Prices'] = Price
print(GG)

Bar=np.array(Bar).astype('str').tolist()

print("The array:", Bar)

这是我的 CSV 代码

Sizes Product Price
10x8 8 .25
:---- 10 .29

在这段代码中,我从 CSV 读取数据。在读取 Bar 数组后,我将该数组转换为数组字符串。我想知道是否有任何方法可以将该字符串数组值分配给 Price 数组中的一个数组值,该数组是整数数组。 几乎我希望代码遍历两个数组并设置str(Bar[i]) = Price[j]。几乎将 Product 列视为变量数组并为它们设置值,以便 ex。 '8' = .25 , '10' = .29 有什么可以使用的库或者什么方法?

【问题讨论】:

    标签: python excel list csv variable-assignment


    【解决方案1】:

    在循环本身中,您可以这样做,

    Product.append("'"+row.get('Product')+"' ="+row.get('Price')) #if u need as string
    
    or
    
    Product.append({ "'"+row.get('Product')+"': "+row.get('Price')+"}") #if u need as dictionary
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多