我有一个excel文件/ csv,它有列和行标题(第1行是所有标题,第A列是所有行标题)。当我提供列和行时,我希望使用dictreader返回(x,y)坐标的值。
最终,我能够提供多个列和一行,它将合并该行的每个给定列中的值。但我会从婴儿步骤开始,因为我目前甚至无法返回我想要的第一个值。以下是我的Excel文件/ CSV的一小部分示例:
PinName RF_Switch_TX1 RF_Switch_TX2 RF_Switch_TX3 RF_Switch_TX3_Scope1 RF_Switch_TX3_Scope2
DM_D_0 1255,1266,1311 1154,1105,
DM_D_1 1256,1266,1311 1154,1105,
DQS 1101,1161 1105 1153,1105
如何构建一个函数,如果提供的引脚名称为“DM_D_1”,列标题为“RF_Switch_TX3_Scope1”,它将返回1154,1105,
我希望只使用dictreader,但我是否需要构建一个搜索文件的迭代函数?
答案 0 :(得分:2)
使用Pandas是否可以接受? (最初的问题提到了Python,但现在似乎没有。)我不确定这是Pandas最惯用的用法,但它似乎做你想做的事。
数据
我将其放入CSV文件中。
PinName,RF_Switch_TX1, RF_Switch_TX2,RF_Switch_TX3,RF_Switch_TX3_Scope1,RF_Switch_TX3_Scope2
DM_D_0,"1255,1266,1311",,,"1154,1105,",
DM_D_1,"1256,1266,1311",,,"1154,1105,",
DQS,,"1101,1161",1105,,"1153,1105"
部分代码
from pandas import read_csv
df = read_csv("/Users/igow/Desktop/so_data.csv")
df = df.set_index(['PinName'])
def get_value(row, col):
return df[col][row]
print(get_value(col='RF_Switch_TX3_Scope1', row='DM_D_1'))
答案 1 :(得分:0)
如果您将数据放入CSV或至少正确指定分隔符,则可以执行以下操作:
In [56]: q = StringIO('''PinName,RF_Switch_TX1, RF_Switch_TX2,RF_Switch_TX3,RF_Switch_TX3_Scope1,RF_Switch_TX3_Scope2
....: DM_D_0,"1255,1266,1311",,,"1154,1105,",
....: DM_D_1,"1256,1266,1311",,,"1154,1105,",
....: DQS,,"1101,1161",1105,,"1153,1105"''')
In [57]: df1 = pd.read_csv(q,)
In [58]: df1.loc[df1['PinName'] == 'DM_D_1']['RF_Switch_TX3_Scope1'].values[0]
Out[58]: '1154,1105,'
In [59]: