擅长:python、mysql、java
<p>我在<a href="https://stackoverflow.com/questions/10715965/add-one-row-in-a-pandas-dataframe">add one row in a pandas.DataFrame</a>的帮助下,使用ShikharDua推荐的方法创建dict列表,每个dict对应于最终数据帧中的一行。在</p>
<p>代码是:</p>
<pre><code>data = pandas.read_csv('input.csv')
dict1 = {"protein":"","drug":"","drug_concentration":""} #should be able to get this automatically using the dataframe columns, I think
rows_list = []
for unique_protein in data.protein.unique():
dict1 = {"protein":unique_protein,"drug":"","drug_concentration":""}
rows_list.append(dict1)
for unique_drug in data.drug.unique():
for unique_drug_conc in data.drug_concentration.unique():
dict1 = {"protein":unique_protein,"drug":unique_drug,"drug_concentration":unique_drug_conc}
rows_list.append(dict1)
df = pandas.DataFrame(rows_list)
df
</code></pre>
<p>它不像我希望的那样灵活,因为不含药物的蛋白质的额外行是硬编码到嵌套的<code>for</code>循环中,但至少这是一个开始。我想我可以在每个<code>for</code>循环中添加一些<code>if</code>语句。在</p>