【发布时间】: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