我有一个dataframe
,我要复制行N次,其中N是另一行的值。
例如,如果这是我的数据帧:
Company WEEK_DAYS SEPTEMBER 15 SEPTEMBER 22 SEPTEMBER 29 value
0 google MON-FRI 0 0 5 0.5
1 google TUE 3 2 0 0.7
因此,我想根据其值复制的列为SEPTEMBER 15
,SEPTEMBER 22
和SEPTEMBER 29
,值应为value
。
所以最终输出应该是这样的:
Company WEEK_DAYS WEEK value
0 google MON-FRI SEPTEMBER 29 0.5
1 google MON-FRI SEPTEMBER 29 0.5
2 google MON-FRI SEPTEMBER 29 0.5
3 google MON-FRI SEPTEMBER 29 0.5
4 google MON-FRI SEPTEMBER 29 0.5
5 google TUE SEPTEMBER 15 0.7
6 google TUE SEPTEMBER 15 0.7
7 google TUE SEPTEMBER 15 0.7
8 google TUE SEPTEMBER 22 0.7
9 google TUE SEPTEMBER 22 0.7
我尝试使用stack
和pivot
- 但我无法获得所需的输出。
任何帮助将不胜感激!
答案 0 :(得分:2)
您可以使用:
set_index
和unstack
重新//Simple PHP
unlink(public_path('storage/image/delete'));
与Storage::delete(['file1.jpg', 'file2.jpg']);
//or
File::delete($image1, $image2, $image3);
repeat
索引,用于按loc
reset_index
用于将Series
转换为列drop
Multiindex
reindex
MultiIndex
答案 1 :(得分:0)
您可以使用pandas.DataFrame.from_records
功能。
我建议您在新的DataFrame中创建一个值列表,即
records = [('google', 'MON-FRI', 'SEPTEMBER 29', '0.5')]
# assumes only one record for MON-FRI .. you might need to do some handling here otherwise
records *= int(df['SEPTEMBER 29'][df[WEEK_DAYS] == 'MON-FRI'])
labels = ['Company', 'WEEK_DAYS', 'WEEK', 'value']
new_df = pd.DataFrame.from_records(records, columns=labels)
我认为这不是最优雅的解决方案,但应该有效。