I have a dataframe with measurement data of different runs at same conditions. Each row contains both the constant conditions the experiment was conducted and all the results from the different runs.
Since I am not able to provide a real dataset, the code snippet provided below will generate some dummy data.
I was able to achieve the desired output, but my function transform_columns()
seems to be unecessary complicated:
import pandas as pd
import numpy as np
np.random.seed(seed=1234)
df = pd.DataFrame(np.random.randint(0, 100, size=(100, 6)), columns=['constant 1', 'constant 2', 1, 2, 3, 4])
def transform_columns(data):
factor_columns = []
response_columns = []
for col in data:
if isinstance(col, int):
response_columns.append(col)
else:
factor_columns.append(col)
collected = []
for index, row in data.iterrows():
conditions = row.loc[factor_columns]
data_values = row.loc[response_columns].dropna()
for val in data_values:
out = conditions.copy()
out['value'] = val
collected.append(out)
df = pd.DataFrame(collected).reset_index(drop=True)
return df
print(transform_columns(df))
Is there any Pythonic or Pandas way to do this nicely?
np.random.seed()
, do you know how to change it? the desired output Can you explain what that is? It's much better for everyone than having to reverse-engineer your code. \$\endgroup\$ – AMC Jan 9 '20 at 23:44