Reputation: 561
I used to have a column "combined" in a dataframe like this:
combined
0 0.97,0.92,0.65,0.24,0.15,0.19
1 0.96,0.86,0.69,0.38,0.56,0.78
2 0.64,0.69,0.93,0.07,0.73,0.05
3 0.29,0.56,0.35,0.76,0.19,0.07
So when I tried to apply a numpy function frequency_changer(), I just need to do like this:
def frequency_changer(y):
x_val_30 = []
for i in range(0, len(y), 4):
x_val_30.append(i)
x_val = []
for i in range(len(y)):
x_val.append(i)
y_val = y
cs = CubicSpline(x_val, y_val)
x2 = cs(x_val_30)
return x2
df['combined']=df['combined'].apply(frequency_changer)
Now, my new dataframe has many columns and each column has a single value.
df_new
0 1 2 3 4 5
0 0.97 0.92 0.65 0.24 0.15 0.19
1 0.96 0.86 0.69 0.38 0.56 0.78
2 0.64 0.69 0.93 0.07 0.73 0.05
3 0.29 0.56 0.35 0.76 0.19 0.07
I applied the same function, it did not work anymore. In the new dataframe, the same function reduced number of total rows. (I want to have a function that works on the length of each rows). How should I solve it? Thank you!
Upvotes: 0
Views: 156
Reputation: 19312
Try using the following. This should give you the same output that you were getting from running the above function on the combined
column.
def frequency_changer(y):
x_val_30 = []
for i in range(0, len(y), 4):
x_val_30.append(i)
x_val = []
for i in range(len(y)):
x_val.append(i)
y_val = y
cs = CubicSpline(x_val, y_val)
x2 = cs(x_val_30)
return x2
#Original df
df['combined'].apply(frequency_changer)
0 [0.97, 0.15]
1 [0.96, 0.56]
2 [0.64, 0.73]
3 [0.29, 0.19]
Name: combined, dtype: object
#For new df
new_out = new_df.T.apply(frequency_changer).T #<------
print(new_out)
0 1
0 0.97 0.15
1 0.96 0.56
2 0.64 0.73
3 0.29 0.19
If you want to combine the 2 columns into one list like your function was behaving for the previous df, then simply use new_out.values.tolist()
and store it in a new dataframe or as a new column.
Upvotes: 1